LearnFrenzy
Sign in Take a free test

Java Programming · Topic 1 of 28

Java Basics, Syntax and Data Types

  • 4 exercises
  • 60 questions
  • ~60 min
  • Answers explained

Coding Techniques

Java Basics, Syntax and Data Types · Study material

Fields have defaults, local variables do not

Instance and static fields start with 0, false, '\u0000' or null; local variables have no default and the compiler refuses to read one before it is definitely assigned. Variables live only inside the block where they are declared.

public class Main {
    // Fields get default values automatically …
    static int count;
    static double rate;
    static boolean active;
    static char initial;
    static String name;          // reference types default to null

    public static void main(String[] args) {
        System.out.println(count + " " + rate + " " + active + " " + (int) initial + " " + name);

        // … but LOCAL variables do not: they must be assigned before use
        int total;
        // System.out.println(total);    compile error: variable total might not have been initialized
        total = 10;
        {
            int inner = total * 2;        // block scope: inner exists only inside these braces
            System.out.println(inner);
        }
        System.out.println(total);
    }
}

Output

0 0.0 false 0 null
20
10

Choosing the right numeric type

DataUse
Counts, indexes, ages, marksint
Phone numbers, IDs, timestamps in ms, file sizeslong (or String for phone numbers)
Measurements, scientific valuesdouble
Moneylong paise/cents or java.math.BigDecimal – never double
Yes/no flagsboolean
Single characterschar
Raw bytes from files or networksbyte

Detecting overflow instead of ignoring it

int a = 2_000_000_000, b = 2_000_000_000;

int wrong = a + b;                 // -294967296: silent wrap-around
long right = (long) a + b;         // 4000000000: widen BEFORE adding
int safe = Math.addExact(a, b);    // throws ArithmeticException: integer overflow

// Same idea for multiplication
long bytes = 1024 * 1024 * 1024 * 4;      // ✘ overflows in int, then widened: 0
long bytes2 = 1024L * 1024 * 1024 * 4;    // ✔ 4294967296

Java basics checklist

  • One public class per file, named like the file.
  • Follow naming conventions: ClassName, methodName, CONSTANT_NAME.
  • Use int by default, long for big counts, double for measurements, BigDecimal for money.
  • Add L and f suffixes; use underscores in long literals.
  • Use var only when the type is obvious from the right-hand side.
  • Make values that never change final.
  • Format output with printf/String.format instead of long + chains.

Ready to test your Programming?

Timed online tests with instant scores and explanations.

About this section

About Java Programming

Java is one of the most used languages in the world – for enterprise software, Android apps, banking systems and backend services. This section covers core Java from syntax and data types to OOP, strings, arrays, exceptions, collections and multithreading, with clear explanations and practice questions for every topic.

Why it matters

  • Java is the main language in service-company placement tests and many product companies
  • OOP, String, collections and exception questions appear in nearly every Java interview
  • Strong core Java is the base for Spring Boot, Android and backend development
  • Java questions are common in university exams and certification tests

Who it is for

  • School students (Class 11–12 / ICSE computer applications) learning Java
  • College students (B.Tech, BCA, MCA) preparing for exams, labs and projects
  • Freshers preparing for placements, Java developer roles and technical interviews
  • Developers revising core Java before an interview

What every topic gives you

  • Study material in five tabs: concepts with runnable examples, coding techniques, programs, interview questions and a cheat sheet
  • MCQs that check your understanding of every concept
  • "Predict the output" puzzles – the trickiest part of written tests
  • "Find the error" programs that train you to spot bugs quickly
  • Complete programs with solutions and real output, verified on a compiler

Asked in

  • Campus placements
  • Java developer interviews
  • Service company tests
  • University exams
  • Certification exams
  • Government IT exams

How to use this section

  1. Read the study material of a topic and run the examples yourself.
  2. Practise the four exercises – MCQs, output, errors, programs – and read every explanation.
  3. Test yourself with the timed online test, then revise the topics you got wrong.

Search LearnFrenzy

Esc

Quick links