Java Basics
Data Types & I/O
Complete lesson for Data Types & I/O using LEARNCODEGURU’s 6-level learning model — from basics to advanced practice, with Java OOP and Collections where relevant.
Basics — Data Types & I/O
Section: Java Basics. Primitive types, size and range, type conversion, and console I/O with System.out and Scanner.
Primitive types
- Eight primitives:
byte,short,int,long,float,double,char,boolean. They are not objects. - Integer literals are
intby default; useLforlong. Floating literals aredoubleby default; usefforfloat. charis an unsigned 16-bit UTF-16 code unit, not a full Unicode code point (supplementary characters need twochars).
| Type | Size | Range / notes | Default (fields) |
|---|---|---|---|
byte | 8-bit | -128 to 127 | 0 |
short | 16-bit | -32,768 to 32,767 | 0 |
int | 32-bit | ≈ ±2.1 billion | 0 |
long | 64-bit | huge integers; suffix L | 0L |
float | 32-bit IEEE | approx. 6–7 digits; suffix f | 0.0f |
double | 64-bit IEEE | approx. 15 digits | 0.0 |
char | 16-bit | 0 to 65535 (UTF-16 code unit) | '\u0000' |
boolean | JVM-defined | true / false | false |
Reference types and local vs field defaults
- Classes, arrays, interfaces, and enums are references. Uninitialized *fields* are
null(or zero/false for primitives). - Local variables have **no** default — you must assign before use or
javacfails. var(Java 10+) infers a local type from the initializer:var name = "Ada";isString. You cannot writevar x;with no initializer.
Console output and Scanner input
System.out.print/println/printfwrite to standard output.Scannerinjava.utilreads tokens fromSystem.in. Close it only if you own the stream; closingSystem.inis often a mistake in small demos.
import java.util.Scanner;
public class IoDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Age: ");
int age = sc.nextInt();
sc.nextLine(); // consume leftover newline
System.out.print("Name: ");
String name = sc.nextLine();
System.out.printf("%s is %d years old%n", name, age);
sc.close();
}
} Widening vs narrowing conversion
- Widening (
int→long→float→double) is implicit. Narrowing (double→int) needs a cast and truncates toward zero. - Integer overflow wraps silently (
Integer.MAX_VALUE + 1isInteger.MIN_VALUE).
Advanced — Data Types & I/O
Scanner pitfalls and numeric precision
nextInt()does not consume the line ending. MixingnextInt()andnextLine()is a classic bug — callnextLine()to clear the rest of the line.next()reads one token (whitespace-delimited);nextLine()reads the rest of the line.float/doubleare binary floating point —0.1 + 0.2is not exactly0.3. UseBigDecimalfor money.- Never use
==to compare doubles for equality in real code; compare with an epsilon orDouble.compare.
Tricky Interview Points
Q1) How many primitive types does Java have?
Answer Eight: byte, short, int, long, float, double, char, boolean. String is a class, not a primitive.
Q2) What is the default value of a local int?
Answer There is none. Locals must be assigned before use. Fields of type int default to 0.
Q3) Why do we write 10L and 1.5f?
Answer Integer literals are int; 10L is long. Floating literals are double; 1.5f is float so it can be assigned to a float variable without a cast.
Q4) Is char signed?
Answer No. char is an unsigned 16-bit UTF-16 code unit.
Q5) What is the difference between print and println?
Answer println appends a newline (the system line separator). print does not.
Q6) Why does nextLine() return empty after nextInt()?
Answer nextInt() leaves the newline in the buffer. The following nextLine() consumes that empty remainder.
Simple Programs
Primitives, cast, and formatted output
public class TypesDemo {
public static void main(String[] args) {
int whole = 10;
double widened = whole; // implicit
int truncated = (int) 9.99; // 9
long big = 3_000_000_000L;
System.out.println(widened);
System.out.println(truncated);
System.out.println(big);
}
} Read two numbers and print the sum
import java.util.Scanner;
public class SumInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println("sum = " + (a + b));
sc.close();
}
} Problem Solving Tricks
- Prefer
intfor whole numbers unless you needlongrange. Preferdoublefor science;BigDecimalfor currency. - After
nextInt()/nextDouble(), callnextLine()before reading a full line. printf("%n")is portable;"\n"is always LF.varonly works for locals with initializers — not for fields or method parameters.
Practice Programs List
- Print the min and max of every primitive using
Integer.MIN_VALUE,Byte.MAX_VALUE, and so on. - Read a first name and age, then print a sentence with
printf. - Cast
130tobyteand explain the wrap-around result. - Show that
0.1 + 0.2 == 0.3is false and print both sides. - Write a program that mixes
nextIntandnextLinecorrectly.