[Java] Java Primer: Types, Classes, and Operators
Categories: Java
Tags: Java Primer
📋 Here are the notes summarizing what I learned from the course!
Java Compiler
Definition
Java is a compiled language, meaning programs are first compiled into bytecode and then executed by the Java Virtual Machine (JVM).
Source Code (.java) → Bytecode (.class) → Executed by JVM
Components of a Java Program
Definition
In Java, the basic building block is the class, which contains methods (functions). The main
method is the entry point for any Java application.
public class Main {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}
Identifiers
Definition
Identifiers are the names used for classes, methods, and variables in Java. They must start with a letter and can include numbers.
int number;
String name;
Base Types
Definition
Java has several primitive data types used for storing simple values directly.
int age = 30;
double price = 19.99;
boolean isActive = true;
Classes and Objects
Definition
A class in Java can be thought of as a blueprint for creating objects (instances).
public class Bicycle {
int gear;
int speed;
void changeGear(int newGear){
gear = newGear;
}
void speedUp(int increment){
speed += increment;
}
}
Creating and Using Objects
Definition
Objects are instances of classes created with the new
keyword.
Bicycle myBike = new Bicycle();
myBike.changeGear(3);
myBike.speedUp(10);
Wrapper Types and Boxing
Definition
Java uses wrapper classes for all primitive types to facilitate operations in object-based contexts.
Integer myInt = 5; // Auto-boxing
int x = myInt; // Auto-unboxing
Method Signatures
Definition
The signature of a method in Java includes its name and parameter types, which helps in identifying the correct method to execute.
public void greet(String name) {
System.out.println("Hello, " + name);
}
Operators
Definition
Java supports various operators for arithmetic, logical operations, and more.
int result = 1 + 2; // Addition
boolean isTrue = (result == 3);
Control Structures
Definition
Java provides control structures such as if-else statements, loops, and switch cases for controlling the flow of execution.
if (isTrue) {
System.out.println("Result is 3");
} else {
System.out.println("Result is not 3");
}
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
Input and Output
Definition
Java uses System.out
for output and System.in
with a Scanner
for input.
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = scanner.nextLine();
System.out.println("Hello, " + name);
Leave a comment