Java in One Shot | Revision - 1 | Output, Input, Variables, Operators | Java Course

Learn Java Basics in a simple way with this one-shot guide. Understand how Java input output, Java variables, and Java operators work together to build basic programs and control application logic.
authorImageVarun Saharawat17 Jun, 2026
Java in One Shot | Revision - 1 | Output, Input, Variables, Operators | Java Course

Learning a new programming language can feel difficult at the start. Many beginners find it hard to understand how code blocks connect, how data moves in a program, and why syntax rules are important. This article makes Java Basics simple by focusing on practical concepts and easy examples.

What is the Core Syntax of Java Basics?

To build any working application in Java programming, you must first understand its basic structure. Java is a class-based and object-oriented language that follows the principle “Write Once, Run Anywhere”, which means the same program can run on any system that has a Java Virtual Machine (JVM).

Every Java program follows a fixed structure. Without this structure, the program will not run. The simplest Java program looks like this:

Java

public class Main {

    public static void main(String[] args) {

        // Your code starts executing here

    }

}

This basic structure is made up of important parts that work together to run the program.

Classes

In Java, all code must be written inside a class. A class is like a container that holds code and defines the structure of the program. The class name usually matches the file name. For example, a class named Main is saved in a file called Main.java.

Main Method

The line public static void main(String[] args) is the starting point of any Java program. This is where execution begins. When you run a Java program, the JVM looks for this method first and starts running the code inside it.

Code Blocks

Curly braces { } are used to define code blocks in Java. They show where a class or method starts and ends. All statements inside these braces belong to that specific block.

Statements

A statement is a single instruction in Java. Every statement ends with a semicolon. If you forget to add a semicolon, the program will show a compilation error.

Understanding this basic structure is the first step in learning Java programming and building more advanced applications. It helps you write clean, organized, and error-free code..

How Does Java Basic Input/Output Work for Beginners?

Interacting with users is a basic part of Java programming. Java input output allows a program to take data from the user and show results on the screen in a simple and clear way.

Printing Text to the Console (Output)

Java provides built-in methods in the System class to print output on the screen. These methods are used to show messages, results, or data.

  • System.out.print(): This prints text on the console but does not move to a new line. The next output continues on the same line.

  • System.out.println(): This prints text and then moves the cursor to a new line. It is useful for clean and structured output.

Java

System.out.print("Hello ");

System.out.println("World!");

System.out.println("Welcome to Java Basics.");

Reading User Values from the Console (Input)

To take input from the user, Java uses the Scanner class. This class is part of the java.util package and reads data from the keyboard using System.in.

Java

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter your name: ");

        String name = scanner.nextLine();

        System.out.print("Enter your age: ");

        int age = scanner.nextInt();

        System.out.println("User: " + name + " | Age: " + age);

        scanner.close();

    }

}

Common Scanner Methods in Java

The Scanner class provides different methods to read different types of input:

  • nextLine(): Reads a full line of text including spaces.

  • next(): Reads only a single word until space.

  • nextInt(): Reads integer values.

  • nextDouble(): Reads decimal numbers.

Understanding these methods helps in handling different types of user input correctly in Java programs.

What Are Variables and Data Types in Java Basics?

Programs need safe containers to hold information while processing tasks. In Java programming, you manage this information using strongly typed data placeholders known as Java variables. Because Java is statically typed, you must declare what kind of data a container holds before you can store values inside it.

Primitive Data Types

Primitive types are predefined, basic units of data built directly into the core language. They differ by the specific amount of memory they use and the range of values they can hold:

Data Type

Memory Allocated

Default Storage Purpose

Example Values

byte

1 byte

Extremely small integer numbers

100, -128

short

2 bytes

Small whole numbers

30000

int

4 bytes

Standard integer values for calculations

100000, -500

long

8 bytes

Exceptionally large whole integers

15000000000L

float

4 bytes

Standard single-precision fractional numbers

3.14f

double

8 bytes

High-precision double-fractional numbers

3.14159265359

char

2 bytes

Single letters, symbols, or Unicode values

'A', '7', '$'

boolean

1 bit

Basic logical state flags

true, false

Non-Primitive Data Types

Non-primitive types refer to objects and complex structures that point to specific memory addresses rather than holding raw values directly. Examples include arrays, custom classes, and the highly popular String object used for handling collections of text characters.

Variable Classification by Scope

The position where you define a container inside your file controls where that data remains accessible:

  • Local Variables: These are declared directly inside a specific method or code block. They only exist while that specific block runs and cannot be accessed outside of it.

  • Instance Variables: These are declared inside a class but outside of any individual method. Each unique object instance maintains its own separate copy of this data.

  • Static Variables: These are defined using the static keyword inside a class block. Every object created from the class shares this exact same variable copy in memory.

What Are Operators and Logic Control in Java Basic?

Once you have stored information inside variables, you need tools to alter, compare, and evaluate those values. You can execute these functions seamlessly by using Java operators.

Arithmetic Operators

These tools perform standard mathematical calculations on numerical variables:

  • Addition (+): Adds values together or links separate text strings.

  • Subtraction (-): Deducts the right value from the left value.

  • Multiplication (*): Multiplies numbers together.

  • Division (/): Divides one number by another. Note that dividing two integers discards the fractional remainder.

  • Modulus (%): Divides two numbers and returns only the remaining fractional leftover value.

Relational Operators

Relational tools compare two values to determine a logical relationship, always returning a result of true or false:

  • Equal to (==): Verifies if two separate values are perfectly identical.

  • Not equal to (!=): Checks if two values are different from each other.

  • Greater than (>) / Less than (<): Compares which numerical value is larger or smaller.

  • Greater than or equal to (>=) / Less than or equal to (<=): Confirms limits or boundary conditions.

Logical Operators

Logical tools combine multiple individual comparison rules to help your program make complex decisions:

  • Logical AND (&&): Evaluates to true only if every single individual condition is true.

  • Logical OR (||): Evaluates to true if at least one of the conditions is true.

  • Logical NOT (!): Reverses the current logical state of a boolean condition (turning true to false and vice versa).

FAQs

What is the difference between print and println in Java input output?

The System.out.print() method prints text on the console and keeps the cursor on the same line. This means the next output will continue on that same line. The System.out.println() method prints the text and then moves the cursor to a new line, making the output more structured and readable.

Why must I declare data types for Java variables?

Java is a statically typed language, which means every variable must have a defined data type before the program runs. The compiler uses this information to understand what kind of data will be stored, allocate proper memory, and detect errors early during compilation instead of at runtime.

How does the modulus operator work in Java?

The modulus operator % returns the remainder after dividing one number by another. For example, 7 % 3 = 1, because 3 goes into 7 two times and leaves a remainder of 1. It is commonly used in programming for checking even/odd numbers and looping logic.

Can I change the size of primitive data types in Java?

No, primitive data types in Java have fixed memory sizes. You cannot change their size after declaration. If a value exceeds the limit of a data type like int, you need to use a larger type, such as long, to store the value.

What happens if I do not close a Scanner object?

If a Scanner object is not closed, it may lead to a resource leak. This means system resources remain occupied unnecessarily, which can reduce performance, especially in larger applications. Closing the Scanner helps free up memory and system resources properly.
Popup Close ImagePopup Open Image
Talk to a counsellorHave doubts? Our support team will be happy to assist you!
Popup Image
avatar

Get Free Counselling Today

and Clear up all your Doubts

Talk to Our Counsellor just by filling out the form.
Student Name
Phone Number
IN
+91
OTP
Email Id
Join 15 Million students on the app today!
Point IconLive & recorded classes available at ease
Point IconDashboard for progress tracking
Point IconLakhs of practice questions
Download ButtonDownload Button
Banner Image
Banner Image