Variables
⇒ Variables are like containers used for storing data values.
# Declaring Variable
int varibaleName = variable_Value;
# Ex :-
int age = 12;
Data Types
⇒
Naming Conventions
⇒ camelCase
→ Start with a lowercase letter. Capitalize the first letter of each subsequent word.
→ Example: myVariableName
⇒ snake_case
→ Start with an lowercase letter. Separate words with underscore
→ Example: my_variable_name
⇒ Kebab-case
→ All lowercase letters. Separate words with hyphens.
→ Example: my-variable-name
⇒ Keep a Good and Short Name
→ Choose names that are descriptive but not too long. It should make it easy to understand the variable's purpose.
→ Example: age, firstName, isMarried
Identifier Rule
⇒ The only allowed characters for identifiers are all alphanumeric characters([A-Z],[a-z],[0-9]), ‘$‘ (dollar sign) and ‘_‘ (underscore).
⇒ Can’t use keywords or reserved words
⇒ Identifiers should not start with digits([0-9])
⇒ Java identifiers are case-sensitive.
⇒ There is no limit on the length of the identifier but it is advisable to use an optimum length of 4 – 15 letters only.
Literals
⇒ Integer
→ 10, 5, -8, etc…
⇒ Floating Points
→ 1.2, 0.25, -0.999, etc…
⇒ Boolean
→ true, false
⇒ Character
→ ‘a’, ‘b’, ‘A’, ‘N’, etc…
⇒ String
→ “hi”, “hello”, etc…
Keywords
⇒ abstract assert boolean break byte case catch char class const* continue default do double else enum extends final finally float for goto* if implements import instanceof int interface long native new package private protected public return short static strictfp super switch synchronized this throw throws transient try void volatile while
<aside> 🔒
const and goto are reserved but not used in Java.</aside>
Escape Sequences
| Escape Sequence | Description | Example Output |
|---|---|---|
\\n |
New line | Moves to next line |
\\t |
Tab (horizontal) | Adds tab space |
\\b |
Backspace | Deletes previous char |
\\r |
Carriage return | Returns to line start |
\\f |
Form feed (rarely used) | Page break (printers) |
\\' |
Single quote | ' |
\\" |
Double quote | " |
\\\\ |
Backslash | \\ |
public class Main {
public static void main(String[] args) {
System.out.println("Hello\\nWorld"); // Line break
System.out.println("Hello\\tWorld"); // Tab space
System.out.println("He said, \\"Hi!\\""); // Double quotes
System.out.println("Path: C:\\\\Java\\\\"); // Backslash
}
}
User Input
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter Your name: ");
String name = input.nextLine();
System.out.print("Welcome " + name);
}
}
Type Conversion and Casting
⇒ Image