Introduction to Java Programming
Java is an object-oriented programming language. It is both a compiled and interpreted language. A Java program is first compiled using the javac compiler, which generates a .class file containing bytecode (if there are no errors). This bytecode is then interpreted and executed by the java interpreter.
Java programs are highly portable. Once compiled, they can run on any platform that has the Java Virtual Machine (JVM) installed. The JVM acts as an execution environment and is available for almost all operating systems.
To write a Java program, you must define a class using the class keyword. Java is case-sensitive, and the class name must exactly match the filename (including case). Java is also a strongly-typed language — you must declare the data type of every variable, and a variable can only hold values compatible with its declared type.
The primitive data types in Java are: byte, short, int, long, float, double, char, and boolean.
Java provides a rich set of built-in classes such as String, ArrayList, HashMap, File, and many more.
A Java program can contain multiple classes, but the public static void main(String[] args) method — the entry point — must be present in one of them. Execution always begins from the main() method.
To compile and run a program (e.g., saved as hello.java):
javac hello.java→ compiles tohello.class(bytecode for JVM)java hello→ executes the program- The filename must match the class name containing
main().
class Demo {
public static void main(String[] param) {
System.out.println("This text will be displayed");
}
}
In the example above, the class name is Demo, so the file must be Demo.java. The main() method must be public (accessible from outside), static (callable without an object), and void (no return value). It accepts a String array as a parameter for command-line arguments.
System.out.println() prints output to the console. Every Java statement ends with a semicolon (;).
Data Types
Data types define what kind of values a variable can hold. Java supports signed integers only (no unsigned types). Here are the primitive types with their sizes and ranges:
| Name | Width in Bits | Description | Range |
|---|---|---|---|
| double | 64 | Double precision floating-point | ±1.7 × 10±308 (approx. 15 decimal digits) |
| float | 32 | Single precision floating-point | ±3.4 × 10±38 (approx. 6–7 decimal digits) |
| char | 16 | Unicode character | '' to '\uFFFF' (0 to 65,535) |
| boolean | — | Logical value | true or false |
| int | 32 | Standard integer | -2,147,483,648 to 2,147,483,647 |
| long | 64 | Large integer | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
| short | 16 | Short integer | -32,768 to 32,767 |
| byte | 8 | Very small integer | -128 to 127 |
Declaring a Variable
type identifier [= value][, identifier [= value], ...];
int a, b, c;
int x = 44, y = 45;
double pi = 3.14159;
char ch = 'x';
class Hypotenuse {
public static void main(String[] param) {
double b = 3.0, p = 5.0;
double hypo = Math.sqrt(b * b + p * p);
System.out.println("Hypotenuse is: " + hypo);
}
}
In this example, b and p are initialized with constants, while hypo is computed dynamically using the Math.sqrt() method.
Scope and Lifetime of Variables
Variables declared inside a block (enclosed in { }) are visible only within that block — this defines their scope and lifetime.
Java has two primary scopes: class-level and method-level. Variables declared in one scope are generally not visible outside it. Scopes can be nested.
class JavaDemo {
public static void main(String[] oops) {
int x;
x = 10;
{
int y = 35;
System.out.println("Sum of " + x + " and " + y + " is: " + (x + y));
x = x + 100;
}
// y = 33; // ERROR - y is out of scope here
System.out.println("Value of x is: " + x); // prints 110
}
}