Loading...

Go Back

Next page
Go Back Course Outline

Java Full Course


Java Basics

Java Syntax and Structure

Java programs follow a specific structure and syntax rules. Every Java application must contain a main method as the entry point.

Basic Java Structure
public class Main {
    public static void main(String[] args) {
        // Your code here
        System.out.println("Hello Java!");
    }
}

Key Syntax Elements:

  • Case Sensitivity - Java is case-sensitive
  • Class Names - Should start with uppercase letter
  • Method Names - Should start with lowercase letter
  • Program File Name - Must match the class name
  • Every statement must end with a semicolon (;)
Data Types and Variables

Java has two categories of data types: primitive and reference types.

Primitive Data Types:

Data Type Size Description
byte 1 byte Stores whole numbers
short 2 bytes Stores whole numbers
int 4 bytes Stores whole numbers
long 8 bytes Stores whole numbers
float 4 bytes Stores fractional numbers
double 8 bytes Stores fractional numbers
boolean 1 bit Stores true/false values
char 2 bytes Stores a single character

Variable Declaration:

Variable Examples
// Declare variables
int age = 25;
double salary = 55000.75;
char grade = 'A';
boolean isJavaFun = true;
String name = "John Doe";

// Output variables
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Salary: $" + salary);
System.out.println("Grade: " + grade);
System.out.println("Java is fun: " + isJavaFun);
Output:
Name: John Doe
Age: 25
Salary: $55000.75
Grade: A
Java is fun: true
Type Casting

Type casting is when you assign a value of one primitive data type to another type.

Widening Casting (Implicit):

Automatically done when passing a smaller size type to a larger size type.

Widening Casting Example
int myInt = 9;
double myDouble = myInt; // Automatic casting: int to double

System.out.println(myInt);    // Output: 9
System.out.println(myDouble); // Output: 9.0

Narrowing Casting (Explicit):

Must be done manually by placing the type in parentheses in front of the value.

Narrowing Casting Example
double myDouble = 9.78;
int myInt = (int) myDouble; // Manual casting: double to int

System.out.println(myDouble); // Output: 9.78
System.out.println(myInt);    // Output: 9
Operators

Java provides various operators for performing operations on variables and values.

Arithmetic Operators:

Arithmetic Operations
int a = 10, b = 4;
System.out.println("a + b = " + (a + b));  // 14
System.out.println("a - b = " + (a - b));  // 6
System.out.println("a * b = " + (a * b));  // 40
System.out.println("a / b = " + (a / b));  // 2
System.out.println("a % b = " + (a % b));  // 2

Relational Operators:

Relational Operations
int x = 5, y = 8;
System.out.println("x == y: " + (x == y));  // false
System.out.println("x != y: " + (x != y));  // true
System.out.println("x > y: " + (x > y));    // false
System.out.println("x < y: " + (x < y));    // true
System.out.println("x >= y: " + (x >= y));  // false
System.out.println("x <= y: " + (x <= y));  // true

Logical Operators:

Logical Operations
boolean p = true, q = false;
System.out.println("p && q: " + (p && q));  // false
System.out.println("p || q: " + (p || q));  // true
System.out.println("!p: " + (!p));          // false

Bitwise Operators:

Bitwise Operations
int m = 5;  // Binary: 0101
int n = 3;  // Binary: 0011

System.out.println("m & n: " + (m & n));   // 1 (0001)
System.out.println("m | n: " + (m | n));   // 7 (0111)
System.out.println("m ^ n: " + (m ^ n));   // 6 (0110)
System.out.println("~m: " + (~m));         // -6 (in two's complement)
System.out.println("m << 1: " + (m << 1)); // 10 (1010)
System.out.println("m >> 1: " + (m >> 1)); // 2 (0010)
Input and Output

Java provides different ways to get input and produce output.

Output with System.out:

Output Methods
System.out.print("Hello ");     // Without newline
System.out.println("World!");  // With newline
System.out.printf("PI: %.2f", Math.PI);  // Formatted output
Output:
Hello World!
PI: 3.14

Input with Scanner:

Scanner Input
import java.util.Scanner;

public class UserInput {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        System.out.print("Enter your name: ");
        String name = scanner.nextLine();
        
        System.out.print("Enter your age: ");
        int age = scanner.nextInt();
        
        System.out.println("Hello " + name + ", you are " + age + " years old.");
        
        scanner.close();
    }
}
Sample Output:
Enter your name: Alice
Enter your age: 30
Hello Alice, you are 30 years old.
Comments

Comments are used to explain Java code and make it more readable.

Single-line Comments:

Single-line Comments
// This is a single-line comment
int x = 5; // You can also comment at the end of a line

Multi-line Comments:

Multi-line Comments
/* This is a multi-line comment
   It can span multiple lines */
   
/**
 * This is a Javadoc comment
 * Used for documentation generation
 * @param args command-line arguments
 */
public static void main(String[] args) {
    // Code here
}

Best Practice: Use comments to explain the "why" behind your code, not the "what". Well-written code should be self-explanatory for the "what".

Keywords and Identifiers

Keywords:

Reserved words that have special meaning in Java. They cannot be used as identifiers.

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

Identifiers:

Names given to classes, variables, methods, etc. They must follow these rules:

Identifier Examples
// Valid identifiers
String firstName;
int _count;
double $balance;
boolean isActive;

// Invalid identifiers
int 2ndValue;     // Starts with digit
String class;     // Uses keyword
double final-score; // Contains hyphen
Go Back

Next page