Arrays and Strings · Introduction to Java Programming

Long Class

Watch lectures

Long Class

1. When Long? (beginner)

long = huge int (8 bytes, ±9 quintillion). For phone numbers, timestamps (System.currentTimeMillis()), IDs. Long = wrapper. Rule: int literal needs L suffix for long!

2. Code — Big Numbers Done Right

// long big = 9000000000; // ERROR! (literal too big for int first!)
long big = 9000000000L; // L forces long literal (correct!)
Long obj = Long.valueOf("9000000000"); // parse big string
System.out.println(obj + " max=" + Long.MAX_VALUE); // 9223372036854775807
System.out.println(System.currentTimeMillis()); // e.g. 1758... (long millis!)
long phone = 9876543210L;
System.out.println(Long.toString(phone)); // back to String!

3. Key Members

Member Use
Long.MAX_VALUE / MIN_VALUE ±9×10¹⁸
Long.parseLong("...") String → long
Long.toString(x) long → String
Long.sum/compare/min/max same as Integer

4. Traps

  • Missing L: long x = 10000000000; fails (compiler reads literal as int first!).
  • Lowercase l looks like 1 — always uppercase L!
  • int + long → long (promotion!).

5. Exam Points

  • L suffix + parse + millis example — 3-5 marks, frequent.
AI LearningWeb Platform

Coursify — Personal AI Course Generator

Struggling with a difficult subject or concept? Turn any engineering topic into a structured course with interactive lessons, quizzes, and research-backed explanations.

← All Introduction to Java Programming modules