Skip to main content

Command Palette

Search for a command to run...

Java Fundamentals: Building Blocks

Published
5 min read
Java Fundamentals: Building Blocks
A

I am an engineering student pursuing a degree in Artificial Intelligence and Data Science at Datta Meghe College of Engineering. I have strong technical skills in Full Stack Web Development, as well as programming in Python and Java. I currently manage Doubtly's blog and am exploring job opportunities as an SDE. I am passionate about learning new technologies and contributing to the tech community.

Java is one of the most popular programming languages out there, and it’s the foundation for building powerful applications. Before diving into advanced topics, we need to start with the fundamentals. Let’s take it step by step.


Understanding Java Keywords

Java keywords are reserved words that have special meaning in the language. They’re like the tools in your toolbox — each one has a specific purpose, and you can’t use them for anything else, like naming your variables.

Common Java Keywords

Here are some commonly used Java keywords:

  • class: Used to define a class.

  • public: An access modifier that makes something available to everyone.

  • static: Used to create methods or variables that belong to the class rather than an instance.

  • void: Indicates that a method does not return any value.

  • if: Used to specify a condition.

  • else: Specifies what happens if the condition is false.

Example:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}
  • public: Everyone can access this class.

  • static: The method belongs to the class, not an object.

  • void: The method doesn’t return anything.

  • class: Defines the class named HelloWorld.

Pro tip: Keywords are case-sensitive. Public or STATIC will throw errors!


Java Variables: Storing Data Effectively

Variables are like containers where you can store data. Think of them like labeled jars in your kitchen — one for sugar, one for salt, etc. In Java, variables have specific types that determine what kind of data they can hold.

Types of Variables:

  1. Local Variables: Declared inside a method and only accessible there.

  2. Instance Variables: Belong to an instance of a class.

  3. Static Variables: Shared among all instances of a class.

Syntax:

dataType variableName = value;

Example:

public class VariableExample {
    int instanceVariable = 10; // Instance variable
    static int staticVariable = 20; // Static variable

    public static void main(String[] args) {
        int localVariable = 30; // Local variable
        System.out.println("Local: " + localVariable);
        System.out.println("Static: " + staticVariable);

        VariableExample obj = new VariableExample();
        System.out.println("Instance: " + obj.instanceVariable);
    }
}

Output:

Local: 30
Static: 20
Instance: 10

Data Types in Java: A Complete Guide

Data types define what kind of data a variable can store. Java is a statically typed language, so you have to declare the data type when you create a variable.

Primitive Data Types:

  1. int: Stores integers (whole numbers) like 1, 42, -99.

  2. double: Stores decimals like 3.14 or 2.718.

  3. char: Stores a single character like 'A' or 'z'.

  4. boolean: Stores true or false.

Non-Primitive Data Types:

  • String: Stores a sequence of characters (like "Hello").

  • Arrays: Stores multiple values of the same type.

Example:

public class DataTypeExample {
    public static void main(String[] args) {
        int age = 25; // Integer
        double price = 19.99; // Decimal
        char grade = 'A'; // Character
        boolean isJavaFun = true; // Boolean
        String message = "Java is awesome!"; // String

        System.out.println("Age: " + age);
        System.out.println("Price: " + price);
        System.out.println("Grade: " + grade);
        System.out.println("Is Java fun? " + isJavaFun);
        System.out.println(message);
    }
}

Output:

Age: 25
Price: 19.99
Grade: A
Is Java fun? true
Java is awesome!

Implicit Conversion in Java: How Data Types Interact

Java can automatically convert smaller data types to larger ones. This is called implicit conversion or type widening. But narrowing conversions (like going from double to int) require explicit casting.

Widening Conversion:

  • Smaller to larger types (e.g., int to double).

Example:

public class ImplicitConversion {
    public static void main(String[] args) {
        int num = 10;
        double result = num; // Implicit conversion
        System.out.println("Integer: " + num);
        System.out.println("Double: " + result);
    }
}

Output:

Integer: 10
Double: 10.0

Narrowing Conversion (Explicit):

public class ExplicitConversion {
    public static void main(String[] args) {
        double num = 9.78;
        int result = (int) num; // Explicit casting
        System.out.println("Double: " + num);
        System.out.println("Integer: " + result);
    }
}

Output:

Double: 9.78
Integer: 9

Adding Comments in Java

Comments make your code easier to understand. They’re like sticky notes for yourself and others who read your code.

Types of Comments:

  1. Single-line comments: Use // for one-line notes.

  2. Multi-line comments: Use /* ... */ for longer notes.

  3. Documentation comments: Use /** ... */ for generating JavaDocs.

Examples:

public class CommentsExample {
    public static void main(String[] args) {
        // This is a single-line comment
        System.out.println("Single-line comment example");

        /*
         * This is a multi-line comment.
         * It spans multiple lines.
         */
        System.out.println("Multi-line comment example");

        /**
         * This is a documentation comment.
         * It’s used for generating JavaDocs.
         */
        System.out.println("Documentation comment example");
    }
}

Output:

Single-line comment example
Multi-line comment example
Documentation comment example

Wrapping It All Up

You’ve just gone through the core building blocks of Java! Here’s a quick recap:

  1. Keywords: Reserved words like class, public, and void.

  2. Variables: Containers for data.

  3. Data Types: Define the kind of data stored in variables.

  4. Implicit Conversion: Java’s way of widening data types.

  5. Comments: Add notes to your code for clarity.

With this solid foundation, you’re ready to take on more complex Java programming. Keep experimenting with the examples above, and you’ll be writing your own Java programs in no time!


Java Jumpstart Series 🌟

Part 11 of 13

Dive into the world of programming with our Java Jumpstart Series—a beginner-friendly, step-by-step guide to mastering Java, one of the most popular and versatile programming languages in the world!

Up next

Getting Started with Java - 01

What is a Programming Language? In simple terms, a programming language is a set of instructions that tells a computer what to do. Think of it like a recipe that guides a chef (the computer) to prepare a dish. Programming languages allow us to intera...

More from this blog

U

UniGeek

29 posts

Unigeek empowers developers and tech enthusiasts at every level. From placement prep and daily coding challenges to advanced upskilling, Unigeek helps you grow in the ever-evolving tech world.