Java CheatSheet | Beginners, Intermediate | Part-1

Basics

Basic syntax and functions from the Java programming language.


Boilerplate

class HelloWorld{ 
public static void main(String args[]){ 
System.out.println("Hello World"); 
} 
}

Showing Output

It will print something to the output console.

System.out.println([text])

Taking Input

It will take string input from the user

import java.util.Scanner; //import scanner class

// create an object of Scanner class
Scanner input = new Scanner(System.in);

// take input from the user
String varName = input.nextLine();

Primitive Type Variables

The eight primitives defined in Java are int, byte, short, long, float, double, boolean, and char those aren't considered objects and represent raw values.

byte

byte is a primitive data type it only takes up 8 bits of memory.

age = 18;

long

long is another primitive data type related to integers. long takes up 64 bits of memory.

viewsCount = 3_123_456L;

float

We represent basic fractional numbers in Java using the float type. This is a single-precision decimal number. Which means if we get past six decimal points, this number becomes less precise and more of an estimate.

price = 100INR;

char

Char is a 16-bit integer representing a Unicode-encoded character.

letter = 'A';

boolean

The simplest primitive data type is boolean. It can contain only two values: true or false. It stores its value in a single bit.

isEligible = true;

int

int holds a wide range of non-fractional number values.

var1 = 256;

short

If we want to save memory and byte is too small, we can use short.

short var2 = 786;

Comments

A comment is the code that is not executed by the compiler, and the programmer uses it to keep track of the code.

Single line comment

// It's a single line comment

Multi-line comment

/* It's a 
multi-line
comment
*/

Constants

Constants are like a variable, except that their value never changes during program execution.

final float INTEREST_RATE = 0.04;

Arithmetic Expressions

These are the collection of literals and arithmetic operators.

Addition

It can be used to add two numbers

int x = 10 + 3;

1 Comments

  1. Wow, Very nice Blog!!!
    Thank you for sharing your article. Great efforts were put to find the list of articles which is very useful to know. This is a great blog. I am pretty much impressed with your good work." Java CheatSheet | Beginners, Intermediate " you put really very helpful information. Keep it up. A web hosting service that offers Each acting as its own independent server. These servers are used to host websites, applications, and other services. If you want your website host. Get now Norway Dedicated Server Hosting. you can ask us for more details and services.

    ReplyDelete
Previous Post Next Post