1. Java Data Types
Explanation:
In Java, a data type defines what kind of value a variable can hold. It tells the compiler how much memory to reserve and how to interpret the data.
Primitive Data Types:
These are the most basic data types. Java has 8 of them:
-
int
– for integers like 10, 200, -5 -
float
,double
– for decimal values like 3.14 -
char
– for single characters like 'A', 'z' -
boolean
– only true or false
Non-Primitive Data Types:
These are more complex types built using primitives:
-
String
– stores text -
Array
– stores multiple values -
Class
,Interface
– used in object-oriented programming
Official Reference:
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
2. Static and Non-Static
Explanation:
In Java, variables and methods can be static or non-static (also called instance members).
Static:
- Shared across all objects.
- You don’t need to create an object to access it.
class Example {
static int x = 10; // static variable
static void show() {
System.out.println(x);
}
}
You can call it like:
Example.show();
Non-Static:
- Each object gets its own copy.
- You must create an object to use it.
class Example {
int y = 20;
void display() {
System.out.println(y);
}
}
Official Reference:
https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html
3. Java Methods
Explanation:
A method is a block of code that performs a specific task. It helps avoid repeating code and makes your program organized.
Syntax:
returnType methodName(parameters) {
// method body
}
Example:
void greet() {
System.out.println("Hello!");
}
This method prints a greeting when called.
Official Reference:
https://docs.oracle.com/javase/tutorial/java/javaOO/methods.html
4. Return Types
Explanation:
A return type defines what value a method will give back after it's done working.
- If it returns nothing, use
void
. - If it returns an
int
,String
, etc., mention the type.
Examples:
int square(int x) {
return x * x;
}
void greet() {
System.out.println("Hi!");
}
In the first example, square
returns a number.
In the second, greet
returns nothing — it just prints a message.
5. Objects in Java
Explanation:
Java is an object-oriented language, meaning you write classes and create objects from them.
A class is like a blueprint.
An object is the real thing built from that blueprint.
Example:
class Dog {
void bark() {
System.out.println("Woof!");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog(); // Object created
d.bark(); // Calling method
}
}
Official Reference:
https://docs.oracle.com/javase/tutorial/java/javaOO/objects.html
6. Local and Global Variables
Explanation:
A variable is used to store data. Depending on where it's declared, it can be either local or global (also called instance variable).
Local Variable:
- Declared inside a method
- Can be used only in that method
Example:
void show() {
int a = 10; // local variable
System.out.println(a);
}
Global Variable:
- Declared outside all methods but inside a class
- Can be used by all methods in the class
Example:
class Test {
int x = 50; // global variable
void printX() {
System.out.println(x);
}
}
Official Reference:
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html
Top comments (0)