DEV Community

rohit juyal
rohit juyal

Posted on

# 📘 Java Basics – More of theory part.

Date: 28 Sept 2025

Welcome back to my Java learning journey!
I am not counting days for blogging as it was creating a unnecessary pressure on me to blog daily. I know it's good but with job it's hard to do coding and most of my time was going into blogging which I could have diverted into coding. Blogging is not waste of time, I learn a lot when I have to explain the topic in my own words. I am just limiting it to once in a week or so.


🔹 Variables in Java

A variable is a name given to a memory location that stores data.

  • Stored in RAM during execution.
  • We must define its data type to indicate what kind of value it will hold.
  • First, we declare a variable, then we may initialise it.
int a;      // declaration
a = 10;     // initialisation
int b = 20; // declaration + initialisation
Enter fullscreen mode Exit fullscreen mode

✅ A variable can only be declared once, but its value can be changed anytime.


🔹 Data Types in Java

Java has two categories of data types:

  1. Primitive (8 types)
  2. Non-Primitive (String, Arrays, Classes, etc.)

🟢 1. Primitive Data Types

These are the most basic types in Java, predefined by the language.

A. Whole Numbers

  • byte
  • short
  • int
  • long

B. Real Numbers (Decimals)

  • float
  • double

C. Single Character

  • char

D. Logical Values

  • boolean

⚠️ Each primitive data type has a predefined range. For example:

byte b = 127;   // max value
byte c = -128;  // min value
Enter fullscreen mode Exit fullscreen mode

🟢 2. Non-Primitive Data Types

These are built using primitive types. Examples:

  • String
  • Array
  • Class

📌 Note:

  • "" → String literal
  • '' → Char literal (cannot be empty)

🔹 Literals

A literal is simply a fixed value that appears exactly as written.

  • Example: 10, "Hello", 'A', true.

👉 String is a data type, and it just means text.


🔹 Statement vs Expression

  • Expression → Anything that produces a value.
  • Statement → A complete command that can be executed (usually ends with ;).

Examples:

3 + 4        // Expression → produces 7
int x = 3+4; // Statement → assigns 7 to variable x
"Hello"      // String literal → an expression
Enter fullscreen mode Exit fullscreen mode

🔹 Keywords

Keywords are reserved words in Java with predefined meaning.
Examples: class, public, static, void, int, if, else.

❌ You cannot use keywords as variable names.


🔹 Output in Java

Using System.out.println()

  • println("Hello") → prints text and moves to the next line.
  • println() → prints a blank line.

Example:

System.out.println("Java is fun!");
System.out.println(); // blank line
System.out.println("Let's learn more...");
Enter fullscreen mode Exit fullscreen mode

🔹 Playing with JShell

JShell is an interactive tool for running Java code without creating a full class.

Some useful commands:

  • /list → shows commands entered so far
  • /var → lists variables created
  • ctrl + c → cancels a process
  • /exit → exits JShell

📌 In JShell, each line is treated as a statement, so ; is optional.


📝 Wrap-up

Today I learned:
✔ What variables are and how to declare/initialize them
✔ Primitive vs Non-primitive data types
✔ Statements vs Expressions
✔ What literals and keywords mean
✔ How to use System.out.println() for output
✔ Some handy JShell commands


Self Notes

Coding is a serious thing, in last two days I wasted much of my time in other activities. I have realised my mistake and now I will code daily. I am already short of time and wasting time isn't good. I won't be scrolling social media now on.

Top comments (0)