Java Syntax and Data Types: A Beginner's Guide

7/19/2025

Java Syntax and Data Types Explained – Beginner Friendly Guide

Go Back

Java Syntax and Data Types: A Beginner's Guide

Java is a strongly typed, object-oriented programming language widely used for building robust applications. Whether you're building web apps, desktop apps, or Android software, understanding Java syntax and data types is fundamental to writing clean, functional code.


  Java Syntax and Data Types Explained – Beginner Friendly Guide

Java Program Structure

Every Java program follows a basic structure involving classes and methods.

Example:

public class MyFirstProgram {
    public static void main(String[] args) {
        System.out.println("Hello, Java!");
    }
}

Syntax Elements:

  • Class Declaration: public class ClassName {}

  • Main Method: public static void main(String[] args) — the entry point

  • Statements: Ended with a semicolon ;

  • Braces {}: Define blocks of code


Java Variables and Data Types

Java variables must be declared with a type. Data types in Java are divided into primitive types and reference types.

Primitive Data Types

Java provides 8 primitive data types:

TypeDescriptionExample
intInteger valuesint age = 25;
floatFloating-point numbersfloat pi = 3.14f;
doubleDouble-precision floatdouble d = 3.14159;
charSingle characterschar ch = 'A';
booleanTrue or falseboolean isJavaFun = true;
byte8-bit integerbyte b = 100;
short16-bit integershort s = 1000;
long64-bit integerlong l = 123456789L;

Reference (Non-Primitive) Data Types

These refer to objects and include:

  • String: String name = "Java";

  • Arrays: int[] nums = {1, 2, 3};

  • Classes and Interfaces: Custom or library-defined types


Type Conversion in Java

Implicit Casting (Widening):

int a = 10;
double b = a;  // Automatically converted

Explicit Casting (Narrowing):

double d = 9.78;
int i = (int) d;  // Needs manual cast

Declaring and Initializing Variables

int number = 10;
float rate = 5.5f;
String name = "Java";
  • Use meaningful names

  • Initialize variables before use

  • Pay attention to type matching


Conclusion

Understanding Java syntax and data types is the first major step toward becoming a proficient Java developer. From writing your first class to using the right variable types, Java's structure is logical and beginner-friendly.

Explore more with conditionals, loops, and object-oriented concepts in upcoming lessons.

Table of content