Log In

Don't have an account? Sign up now

Lost Password?

Sign Up

Prev Next

Java Basics

In this module, learners move from understanding Java fundamentals to actually writing meaningful Java code. Java Basics introduce how data is stored, processed, and displayed in a Java program. Concepts such as variables, data types, operators, type casting, input/output operations, and coding conventions are essential for building clean, readable, and efficient Java applications. Mastering these basics ensures a strong foundation for advanced topics like control statements, object-oriented programming, and data structures.


1. Variables in Java

What is a Variable?

A variable is a named memory location used to store data that can change during program execution. Every variable in Java must be declared with a specific data type before use.

Key Characteristics of Variables

  • Stores values temporarily in memory
  • Has a unique name (identifier)
  • Must be declared before use
  • Value can be changed during execution

Types of Variables

  • Local Variables
    Declared inside methods and accessible only within that method.
  • Instance Variables
    Declared inside a class but outside methods; associated with objects.
  • Static Variables
    Declared using the static keyword; shared across all objects of a class.

2. Data Types in Java

Data types define what kind of data a variable can store and how much memory it occupies. Java data types are divided into primitive and reference data types.


2.1 Primitive Data Types

Primitive data types store simple values directly in memory. Java has eight primitive data types.

Data TypeDescription
byteSmall integer values
shortMedium integer values
intMost commonly used integer
longVery large integers
floatDecimal values (single precision)
doubleDecimal values (double precision)
charSingle character
booleanTrue or false

Key Points

  • Faster than reference types
  • Fixed memory size
  • Cannot store complex objects

2.2 Reference Data Types

Reference data types store memory addresses of objects rather than actual values.

Examples

  • String
  • Arrays
  • Classes
  • Interfaces

Key Points

  • Used to store complex data
  • Supports object-oriented programming
  • Default value is null

3. Operators in Java

Operators are symbols used to perform operations on variables and values.


3.1 Arithmetic Operators

Used for mathematical calculations.

OperatorOperation
+Addition
-Subtraction
*Multiplication
/Division
%Modulus

3.2 Relational Operators

Used to compare values and return boolean results.

OperatorDescription
==Equal to
!=Not equal to
>Greater than
<Less than
>=Greater than or equal
<=Less than or equal

3.3 Logical Operators

Used to combine boolean expressions.

OperatorMeaning
&&Logical AND
`
!Logical NOT

3.4 Bitwise Operators

Used to perform operations at the bit level.

OperatorOperation
&Bitwise AND
``
^Bitwise XOR
~Bitwise Complement
<<Left shift
>>Right shift

Use Case: Low-level programming, performance optimization.


4. Type Casting and Type Conversion

Type casting is the process of converting one data type into another.


4.1 Implicit Type Casting (Widening)

  • Automatically done by Java
  • Converts smaller data type to larger data type

Example:
int → long → float → double


4.2 Explicit Type Casting (Narrowing)

  • Done manually by the programmer
  • Converts larger data type to smaller data type
  • May cause data loss

Example:
double → int


Key Differences

ImplicitExplicit
AutomaticManual
No data lossPossible data loss
SafeRisky

5. Input and Output Operations

Input and output operations allow interaction between the user and the program.


5.1 Output in Java

  • System.out.print() – prints output
  • System.out.println() – prints output with new line

Used to display messages, results, and debugging information.


5.2 Input in Java

Java uses the Scanner class to read user input.

Common Inputs

  • Integer values
  • Decimal values
  • Strings
  • Characters

Key Points

  • Scanner belongs to java.util package
  • Reads input from keyboard

6. Comments in Java

Comments are used to explain code and improve readability. They are ignored by the compiler.


Types of Comments

  1. Single-line Comment
    Used for short explanations.
  2. Multi-line Comment
    Used for detailed explanations.
  3. Documentation Comment (/** */)
    Used to generate Java documentation (Javadoc).

7. Naming Conventions in Java

Naming conventions improve code readability and professionalism.

Standard Java Naming Rules

  • Class Names: PascalCase
    Example: StudentDetails
  • Variable & Method Names: camelCase
    Example: totalMarks
  • Constants: UPPERCASE
    Example: MAX_VALUE
  • Packages: lowercase
    Example: com.company.project

Why Naming Conventions Matter

  • Makes code easy to read
  • Helps in team collaboration
  • Follows industry standards
  • Improves maintainability

Learning Outcomes (End of Week 4)

By completing this module, learners will:

  • Understand how Java stores and processes data
  • Use variables and data types confidently
  • Apply operators correctly
  • Perform type casting safely
  • Take user input and display output
  • Write clean, readable, professional Java code

Practice Activities

  • Write programs using all data types
  • Perform arithmetic and logical operations
  • Create input-based programs
  • Apply proper naming conventions
  • Add meaningful comments to code

Example:

java

import java.util.Scanner;

public class BasicOperations {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        // Variables and data types
        int age = 25;
        double salary = 50000.50;
        char grade = 'A';
        boolean isEmployed = true;
        
        // Input
        System.out.print("Enter your name: ");
        String name = sc.nextLine();
        
        // Operators
        int x = 10, y = 3;
        System.out.println("Sum: " + (x + y));
        System.out.println("Division: " + (x / y));
        System.out.println("Modulus: " + (x % y));
        
        // Type casting
        double result = (double) x / y;
        System.out.println("Precise division: " + result);
    }
}

Leave a Comment

    🚀 Join Common Jobs Pro — Referrals & Profile Visibility Join Now ×
    🔥