Java
Java is a high-level, object-oriented programming language that is widely
used for developing applications for Web, Desktop, Mobile, Games,
Cloud based services.
Key Features:
Platform Independent
Object oriented
Simple and easy to learn
Secure
Robust
Multithreading
Community and Ecosystem
Variables:
Variables in Java are named storage locations that hold data. They are
fundamental to programming, allowing you to store and manipulate values
during program execution.
Data Types:
In Java, data types specify the type of data that a variable can hold. Java is a
strongly typed language, which means every variable must be declared with a
data type.
🔹 Java Data Types are divided into two main categories:
1. Primitive Data Types
These are the most basic data types built into the language.
Type Size Description
Byte 1 byte Stores whole
numbers from -
128 to 127
Short 2 bytes Whole numbers
from -32,768 to
32,767
Int 4 bytes Whole numbers
from -2B to 2B
Long 8 bytes Larger whole
numbers
Float 4 bytes Decimal numbers
(less precision)
Double 8 bytes Decimal numbers
(more precision)
Char 2 bytes A single character
Boolean 1 bit True or False
Values
2. Non-Primitive (Reference/Object) Data Types
These refer to objects and classes. They can be custom or built-in types.
Type Description:
String Represents a sequence of characters ("Hello")
ArraysA collection of similar data elements
Class Blueprint for creating objects
Interface Defines methods a class must implement
Enum A special class for constants
Scanner class:
The Scanner class is used to take input from the user in Java.
It is part of the java.util package.
🔸 To Use Scanner:
import java.util.Scanner;
🔸 Create Scanner Object:
Scanner Sc = new Scanner(System.in);
🔸 Common Scanner Methods:
next()
Description: Reads a single word (token). It stops reading when a space is
encountered.
Example Input: If the user types "Hello World", this method will only
read "Hello".
Usage: String word = sc.next();
nextLine()
Description: Reads an entire line of input including spaces until the user
presses Enter.
Example Input: "Hello World" will be read fully as "Hello World".
Usage: String line = sc.nextLine();
nextInt()
Description: Reads an integer value from the input.
Example Input: 42
Usage: int num = sc.nextInt();
nextDouble()
Description: Reads a double (decimal) number.
Example Input: 3.14
Usage: double d = sc.nextDouble();
nextBoolean()
Description: Reads a boolean value (true or false).
Example Input: true
Usage: boolean b = sc.nextBoolean();
nextFloat()
Description: Reads a float value.
Example Input: 2.5
Usage: float f = sc.nextFloat();
nextLong()
Description: Reads a long integer value.
Example Input: 12345678900
Usage: long l = sc.nextLong();
char ch = sc.next().charAt(0);
Java Operators:
1. Arithmetic Operators
Used for basic math operations.
+ : Addition (a + b)
- : Subtraction (a - b)
* : Multiplication (a * b)
/ : Division (a / b)
% : Modulus (a % b)
2. Relational (Comparison) Operators
Used to compare two values.
== : Equal to (a == b)
!= : Not equal to (a != b)
> : Greater than (a > b)
< : Less than (a < b)
>= : Greater or equal (a >= b)
<= : Less or equal (a <= b)
3. Logical Operators
Used to combine multiple conditions.
&& : Logical AND (a > 5 && b < 10)
|| : Logical OR (a > 5 || b < 10)
! : Logical NOT (!(a > b))
4. Assignment Operators
Used to assign values to variables.
= : Assign (a = 10)
+= : Add and assign (a += 5)
-= : Subtract and assign (a -= 3)
*= : Multiply and assign (a *= 2)
/= : Divide and assign (a /= 4)
%= : Modulus and assign (a %= 3)
5. Unary Operators
Used with one operand.
+ : Unary plus (+a)
- : Unary minus (-a)
++ : Increment (a++, ++a)
-- : Decrement (a--, --a)
! : Logical NOT (!true)
6. Bitwise Operators
Used for bit-level operations.
& : Bitwise AND (a & b)
| : Bitwise OR (a | b)
^ : Bitwise XOR (a ^ b)
~ : Bitwise Complement(~a)
<< : Left shift (a << 2)
>> : Right shift (a >> 2)
7. Ternary Operator
Shorthand for if-else.
Syntax: condition ? value_if_true : value_if_false;
Example: int max = (a > b) ? a : b;
String Methods:
Name = “J a v a”
0123
Name.length();
It return the length of the String.
str.toLowerCase();
It converts the String into Lower case
JAVA = java
str.toUpperCase();
It converts the String into Upper case
java = JAVA
str.trim();
It removes leading and trailing spaces
str.substring(int start);
It returns the substring from start to end
Program
Str.substring(3,5);
str.substring(int start, int end);
It returns the substring from given start to end index
str.replace();
It replaces the String or char with the new string or char
str.startsWith(“am”);
It returns the Boolean value after checking the string starts with same
char or string
str.endsWith();
It returns the Boolean value after checking the string ends with same
char or string
str.charAt(5);
It returns the char that present in that Index
program
str.indexOf(‘a’);
It returns the index value of that char
Str.contact();
Hello
World
str.lastIndexOf();
It returns the index value of that char from last
str.indexOf(‘p’, 3);
It returns the index value of ‘p’ starting from 3rd index
str.equals();
It returns Boolean value after checking the given string equals to it or not
NOTE: It is case sensitive
str.equalsIgnoreCase();
It returns Boolean value after checking the giving string equals to it or
not.