public class Main {
public static void main(String[] args) {
System.out.println("Hello World");
}
}- Every Java program must be inside a class.
- The class name should start with an uppercase letter (e.g.,
Main). - The file name must match the class name (e.g.,
Main.java).
- Java is case-sensitive:
MyClassandmyclassare different.
- Save the file as
Main.java. - Compile and run using Java tools (ensure Java is installed).
Hello World- Required entry point of every Java program.
public static void main(String[] args)- Starts with
//
// This is a comment
System.out.println("Hello World");- Variables are containers for data.
- Each variable has a type and name.
String→ Text,"Hello"int→ Integer numbers,123float→ Decimal numbers,19.99fchar→ Single character,'A'boolean→ True/false values
- All Java variables must be identified with unique names.
- These unique names are called identifiers.
- Identifiers can be:
- Short names (like
xandy) - Descriptive names (like
age,sum,totalVolume)
- Short names (like
- It is recommended to use descriptive names to make code understandable and maintainable.
// Good
int minutesPerHour = 60;
// OK, but not so easy to understand what m actually is
int m = 60;Java has two main groups of primitive number types:
Store whole numbers (positive or negative) without decimals.
byteshortintlong
- Stores numbers from -128 to 127
- Useful to save memory if values fit in this range
byte myNum = 100;
System.out.println(myNum);- Stores numbers from -32,768 to 32,767
short myNum = 5000;
System.out.println(myNum);- Stores numbers from -2,147,483,648 to 2,147,483,647
- Most commonly used integer type
int myNum = 100000;
System.out.println(myNum);- Stores numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
- Use when
intrange is exceeded - End literal with
L
long myNum = 15000000000L;
System.out.println(myNum);double myNum = 19.99d;
System.out.println(myNum);- The precision of a floating point value indicates how many digits it can have after the decimal point.
floatprecision: about 6-7 decimal digits.doubleprecision: about 16 decimal digits.- Therefore, it is safer to use
doublefor most calculations where precision matters.
- Floating point numbers can be represented in scientific notation using an
"e"or"E"to indicate powers of 10.
float f1 = 35e3f;
double d1 = 12E4d;
System.out.println(f1);
System.out.println(d1);Very often in programming, you will need a data type that can only have one of two values, like:
- YES / NO
- ON / OFF
- TRUE / FALSE
For this, Java has a boolean data type, which can only take the values true or false.
boolean isJavaFun = true;
boolean isFishTasty = false;
System.out.println(isJavaFun); // Outputs true
System.out.println(isFishTasty); // Outputs falseThe char data type is used to store a single character.
The character must be surrounded by single quotes, like 'A' or 'c'.
char myGrade = 'B';
System.out.println(myGrade);Alternatively, if you are familiar with ASCII values, you can use those to display certain characters:
char myVar1 = 65, myVar2 = 66, myVar3 = 67;
System.out.println(myVar1); // Outputs: A
System.out.println(myVar2); // Outputs: B
System.out.println(myVar3); // Outputs: CThe String data type is used to store a sequence of characters (text).
String values must be surrounded by double quotes.
String greeting = "Hello World";
System.out.println(greeting);-
The
Stringtype is so commonly used and integrated in Java, some call it "the special ninth type." -
A
Stringin Java is actually a non-primitive data type, because it refers to an object. -
The
Stringobject has methods to perform operations on strings. -
Don’t worry if you don’t understand objects yet — this will be covered in later chapters.