Java Tutorial
Java Data Types
int, double, char, boolean, and String each store a different kind of value. Picking the right one is the first correctness decision you make in any program.
The primitive types
Java has eight built-in primitive types. In everyday code you reach for four of them:int, double, boolean, and char. The rest matter when size or precision does.
| Type | Holds | Example |
|---|---|---|
int | Whole numbers (±2.1 billion) | int n = 7; |
long | Big whole numbers | long big = 9000000000L; |
double | Decimal numbers | double x = 3.5; |
boolean | true or false | boolean ok = true; |
char | One character, single quotes | char g = 'B'; |
Example
public class Main {
public static void main(String[] args) {
int n = 7;
double x = 3.5;
char grade = 'B';
boolean ok = true;
System.out.println(n + " " + x + " " + grade + " " + ok);
}
}A long literal needs an L suffix (9000000000L); a plain9000000000 is read as an int and overflows before it is even assigned.
String is a class, not a primitive
String holds text in double quotes. It is a full object with methods like length()and toUpperCase(). Join strings with +.
Example
public class Main {
public static void main(String[] args) {
String name = "Ada";
System.out.println(name.length()); // 3
System.out.println(name.toUpperCase()); // ADA
}
}The number-one Java beginner trap: compare strings with .equals(), not==. == asks "are these the same object in memory?", which is often false even when the text matches. a.equals(b) asks "is the text the same?" — that's almost always what you want.
Example
public class Main {
public static void main(String[] args) {
String a = "hi";
String b = new String("hi");
System.out.println(a == b); // false (different objects)
System.out.println(a.equals(b)); // true (same text)
}
}Integer division and casting
Dividing two ints throws the fraction away — 17 / 5 is 3, not3.4. Make one side a double to keep the decimals. Going the other way,(int) drops the fraction on purpose.
Example
public class Main {
public static void main(String[] args) {
System.out.println(17 / 5); // 3 (int division truncates)
System.out.println(17 / 5.0); // 3.4 (one double keeps decimals)
System.out.println((int) 9.8); // 9 (cast drops the fraction)
}
}This bites in averages: (a + b) / 2 with two ints truncates. Write (a + b) / 2.0when you want the real average.
Wrapper types and overflow
Each primitive has a matching object type — Integer for int, Double fordouble — used when you need objects (for example inside an ArrayList). They also carry helpers like Integer.parseInt("42") to turn text into a number.
An int tops out near 2.1 billion. Add past that and it silently wraps to a negative number (overflow) — no error. Reach for long when a value could get large, such as a running total or a factorial.
Try It Yourself
Exercise: Predict the output, then explain the surprise.
System.out.println(5 / 2);
System.out.println(5 / 2.0);Show solution
The first prints 2 — both operands are int, so the division truncates. The second prints 2.5 — the 2.0 makes it floating-point division. Same numbers, different types, different answer.
Key Takeaways
- Use
int,double,boolean,charfor everyday values;longfor big numbers. Stringis a class — compare text with.equals(), never==.- Int-by-int division truncates; make one side a
doubleto keep the fraction. intoverflows silently past ~2.1 billion — uselongwhen values can grow.
Worked examples
The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.
Open them in the Java editor at /java/try. Change one measurement and check whether the result still has the right unit.
Maths
Integer vs double division
17 / 5 is 3 in integer arithmetic. 17 / 5.0 is 3.4. The type of the operands decides the kind of division.
Example
public class Main {
public static void main(String[] args) {
System.out.println(17 / 5);
System.out.println(17 / 5.0);
}
}