Basic Data Types
- Java has built in primitive data types like int, long, char etc.
- Also, it has wrapper classes for these primitive types, eg. Integer, Long etc.
- Java provides Autoboxing and Unboxing for these types.
- Java has built-in array. Although on most scenarios Collection Framework's ArrayList will be good alternative.
- It has String data type.
- Also, Java has Class, Interface and Methods.
// Primitive data types
int anInt = 1000;
long aLong = 100000L;
float aFloat = 10.5f;
double aDouble = 20.99;
boolean aBoolean = true;
char ch = 'a';
// Reference (Object) data types
// string
String str = "Hello world";
//array
int[] numbers = {1, 2, 3, 4, 5};
for (int i = 0; i < numbers.length; i++) {
System.out.println("Element at index " + i + ": " + numbers[i]);
}
// Wrapper classes : wrapper for primitive data types
Integer a = 12;
Long b = 11L;
Float c = 12.2f;
Double d = 12.2;
Character e = 'c';
Boolean f = true;
/*
Java provides autoboxing and unboxing features, which automatically convert between
primitive types and their corresponding wrapper classes.
*/
int num1 = 10;
Integer intObj = num1; // Autoboxing: primitive int to Integer
int unboxedInt = intObj; // Unboxing: Integer to primitive int
Class and Method
// A simple class definition
class Person {
// Instance variables (fields)
private String name; // Private access modifier
private int age; // Private access modifier
long value = 12l; // Default access modified
// Constructor method to initialize the object
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// Method with default access modifier (accessible within this class and within same package)
void sayHello() {
System.out.println(name + " is saying hello");
}
// Method with public access modifier
public void introduce() {
System.out.println("Hello, my name is " + name + " and I am " + age + " years old.");
}
// Method with private access modifier (only accessible within this class)
private void secretMethod() {
System.out.println("This is a secret method.");
}
// Getter method with public access modifier
public String getName() {
return name;
}
// Setter method with public access modifier
public void setName(String name) {
this.name = name;
}
// Main method - entry point of the program
public static void main(String[] args) {
// Creating an object of Person class
Person person = new Person("Alice", 30);
// Calling the public method
person.introduce();
// Accessing getter method
System.out.println("Person's name is: " + person.getName());
}
}
Rules for class:
- One java file can contain only one public class. File name should be same as the public class name, eg. Person.java
Access Modifiers:
Name | Description |
public | The public access modifier allows the class, method, or variable to be accessed from anywhere—inside or outside the package |
private | The private access modifier restricts the visibility of methods and variables to the class in which they are defined. |
protected | The protected access modifier allows the class, method, or variable to be accessed within the same package and by subclasses (even if they are in different packages). |
default (no modifier) | Members with default access are visible only within the same package, but not from outside the package. This is often used for methods and variables that are meant to be package-private. |
Interface: An interface is a completely "abstract class" that is used to group related methods with empty bodies
interface Vehicle {
void start(); // abstract method
void stop(); // abstract method
}
class Car implements Vehicle {
@Override
public void start() {
System.out.println("Car is starting");
}
@Override
public void stop() {
System.out.println("Car is stopping");
}
}