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)