Java Basics

Java Version Command

java --version

Java Program

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

Compile and run code

javac example.java
java Example

Declaring Variables

variables.java
Copy
String name = "John Doe";
int age = 10;
float experience = 12.5F;
char gender = 'M';
boolean enableAccess = true;

Data Types

Data Type Size Default Range
byte 1 byte 0 -128 to 127
short 2 bytes 0 -32,768 to 32,767
int 4 bytes 0 -2,147,483,648 to 2,147,483,647
long 8 bytes 0 -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float 4 bytes 0.0f 1.4E-45 to 3.4028235E38
double 8 bytes 0.0d 4.9E-324 to 1.7976931348623157E308
boolean 1 bit false true / false
char 8 bytes \u0000 0 to 65535

Arithmetic Operators


Operator Name Functions Example
+ Addition Add two values x + y
- Subtraction Subtracts a value from another x-y
* Multiplication Multiplies two values x*y
/ Division Divides one value by another x / y
% Modulus Returns the remainder x % y
++ Increment Increases value of a variable by 1 ++x
-- Decrement Decreases value of a variable by 1 --x

Relational Operators


Operator Name Functions Example
== Equal to Returns true if both the operands are referring to the same object, otherwise false x == y
!= Not equal to Returns false if both the operands are referring to the same object, otherwise true x != y
> Greater than Returns true if value of left operands is greater than right, otherwise false x > y
< Less than Returns false if value of left operands is less than right, otherwise true x < y
>= Greater than and equals Returns true if value of left operands is greater than and equal to right, otherwise false x >= y
<= Less than and equals Returns false if value of left operands is less than and equal to right, otherwise true x <= y

Logical Operators


Operator Name Functions Example
&& Logical And Returns true if both the operands evaluates to true, otherwise false x && y
|| Logical OR Returns true if one either one of the operands evaluates to true, otherwise true x || y

Ternary Operator ( ? : )


variable = expression1 ? expression2: expression3

Java
Copy
int a = 5;
int b = 0;

b = (a > 0) ? a * 2: 0;

//Output will be 10
System.out.println(b);

If Else


Java
Copy
int x = 5;
int y = 10;
if(x > y) 
{
	System.out.println("x is greater");
} 
else if(y > x) 
{
	System.out.println("y is greter");
}
else 
{
	System.out.println("x and y are equal");
}

// Output : y is greater

Switch


Java
Copy
int number = 4;
String output;
switch (number)
{
	case 1:
		output = "One";
		break;
	case 2:
		output = "Two";
		break;
	case 3:
		output = "Three";
		break;
	case 4:
		output = "Four";
		break;
	default:
		output = "Not between 1 - 4";
		break;
}

System.out.println("Selected number : " + output);
// Output: Selected number : Four

For Loop


Java
Copy
for (int i=0; i<5; i++)
	{
		System.out.println("The number is : " + i);
	}

Output:



The number is : 0
The number is : 1
The number is : 2
The number is : 3
The number is : 4

For Each Loop


Java
Copy
String[] fruits = {"Apple","Mango","Grape","Cherry"};
for (String fruit: fruits)
{
     System.out.println(fruit);
}


Output:



Apple
Mango
Grape
Cherry

Break


Java
Copy
for (int i=0; i<5; i++)
{
	if(i==4)
	{
		System.out.println("Breaking loop at : " + i);
		break;
	}
	System.out.println("The number is : " + i);
}

Output:



The number is : 0
The number is : 1
The number is : 2
The number is : 3
Breaking loop at : 4

Continue


Java
Copy
for (int i=0; i<5; i++)
{
	if(i==2)
	{
		System.out.println("Skipping number");
		continue;
	}
	System.out.println("The number is : " + i);
}

Output:



The number is : 0
The number is : 1
Skipping number
The number is : 3
The number is : 4

While Loop


Java
Copy
int counter = 0;
while (counter < 5)
{
	System.out.println("The count is : " + counter);
	counter++;
}

Output:



The count is : 0
The count is : 1
The count is : 2
The count is : 3
The count is : 4

Do While


Java
Copy
int counter = 0;
do{
	System.out.println("The count is : " + counter);
	counter++;
}while (counter < 5);

Output:



The count is : 0
The count is : 1
The count is : 2
The count is : 3
The count is : 4

User Input


Java
Copy
System.out.print("Enter your message : ");
Scanner in = new Scanner(System.in);
String input = in.nextLine();
System.out.println("You entered : " + input);

Output:



Enter your message : Welcome
You entered : Welcome

Access Modifiers

Modifier Class Package Subclass Global
public Y Y Y Y
protected Y Y Y N
default Y Y N N
private Y N N N