1.Introduction
Programming languages come and go. Technologies change every year.
But even after decades, Java still powers millions of applications around the world — from banking systems and Android apps to enterprise software used by giant companies.
The interesting part?
Every advanced Java application starts with just a few basic concepts.
Before learning frameworks, APIs, or backend development, every Java developer must first understand:
How classes are declared
What data types actually mean
How fields store and manage data inside objects
These may look like beginner topics, but they form the backbone of Java programming. If these fundamentals are strong, learning advanced concepts becomes much easier.
In this blog, we’ll break down Java classes, primitive and non-primitive data types, and fields in a simple and beginner-friendly way with clear examples.
2.Classes, Data Types, and Fields in Java
Java is one of the most popular programming languages in the world. From Android apps to enterprise software, Java is used everywhere because of its simplicity, security, and platform independence.
Before building real-world applications, every Java developer must understand three important fundamentals:
How to declare a class correctly
Types of data types in Java
What fields are and how they work
Let’s explore them one by one.
3. Rules for Declaring a Class in Java
A class is the blueprint of an object.
In Java, everything revolves around classes.
Basic Syntax
class Student {
}
4.Important Rules for Declaring a Class
1. Class Name Should Start with a Letter
Correct:
class Employee {
}
Wrong:
class 123Employee {
}
2. Java is Case Sensitive
class Student {
}
class student {
}
Both are considered different classes.
3. Spaces Are Not Allowed in Class Names
Wrong:
class My Class {
}
Correct:
class MyClass {
}
4. Special Characters Are Not Allowed (Except _ and $)
Correct:
class Student_Data {
}
Wrong:
class Student@Data {
}
5. File Name and Public Class Name Must Be Same
public class Car {
}
The file name must be:
Car.java
Otherwise Java will throw an error.
4.Data Types in Java
Data types define what kind of value a variable can store.
Java mainly has two categories of data types:
Primitive Data Types
Non-Primitive Data Types
Primitive Data Types
Primitive data types are predefined by Java.
They store simple values directly.
Java has 8 primitive data types.
1. byte
The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). The byte data type can be useful for saving memory in large arrays, where the memory savings actually matters. They can also be used in place of int where their limits help to clarify your code; the fact that a variable's range is limited can serve as a form of documentation.
byte age = 25;
Size: 1 byte
2. short
The short data type is a 16-bit signed two's complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive). As with byte, the same guidelines apply: you can use a short to save memory in large arrays, in situations where the memory savings actually matters.
short salary = 20000;
Size: 2 bytes
3. int
By default, the int data type is a 32-bit signed two's complement integer, which has a minimum value of -231 and a maximum value of 231-1. In Java SE 8 and later, you can use the int data type to represent an unsigned 32-bit integer, which has a minimum value of 0 and a maximum value of 232-1. Use the Integer class to use int data type as an unsigned integer. See the section The Number Classes for more information. Static methods like compareUnsigned, divideUnsigned etc have been added to the Integer class to support the arithmetic operations for unsigned integers.
int marks = 95;
Size: 4 bytes
4. long
The long data type is a 64-bit two's complement integer. The signed long has a minimum value of -263 and a maximum value of 263-1. In Java SE 8 and later, you can use the long data type to represent an unsigned 64-bit long, which has a minimum value of 0 and a maximum value of 264-1. Use this data type when you need a range of values wider than those provided by int. The Long class also contains methods like compareUnsigned, divideUnsigned etc to support arithmetic operations for unsigned long.
long population = 8000000000L;
Size: 8 bytes
5. float
The float data type is a single-precision 32-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification. As with the recommendations for byte and short, use a float (instead of double) if you need to save memory in large arrays of floating point numbers. This data type should never be used for precise values, such as currency. For that, you will need to use the java.math.BigDecimal class instead. Numbers and Strings covers BigDecimal and other useful classes provided by the Java platform.
float price = 99.5f;
Size: 4 bytes
6. double
The double data type is a double-precision 64-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification. For decimal values, this data type is generally the default choice. As mentioned above, this data type should never be used for precise values, such as currency.
double pi = 3.141592653;
Size: 8 bytes
7. char
The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).
char grade = 'A';
Size: 2 bytes
8. boolean
The boolean data type has only two possible values: true and false. Use this data type for simple flags that track true/false conditions. This data type represents one bit of information, but its "size" isn't something that's precisely defined.
boolean isJavaFun = true;
Non-Primitive Data Types
Non-primitive data types are created by programmers or provided by Java.
They store references instead of actual values.
Examples:
String
Arrays (TBD)
Classes
Interfaces (TBD)
Objects
Non-primitive data types are called reference types because they refer to objects.
The main differences between primitive and non-primitive data types are:
Primitive types in Java are predefined and built into the language, while non-primitive types are created by the programmer (except for String).
Non-primitive types can be used to call methods to perform certain operations, whereas primitive types cannot.
Primitive types start with a lowercase letter (like int), while non-primitive types typically starts with an uppercase letter (like String).
Primitive types always hold a value, whereas non-primitive types can be null.
String Example
String name = "Hari";
Class Object Example
class Student {
}
Student s1 = new Student();
5.Final Takeaway
Every expert Java developer once started with the basics.
Understanding classes, data types, and fields may seem simple at first, but these concepts are the foundation of everything you build in Java.
A class gives structure to your program.
Data types help Java understand and manage data efficiently.
Fields define the properties that make every object unique.
When your fundamentals are strong, learning advanced topics like objects, constructors, inheritance, collections, JDBC, Spring Boot, and backend development becomes much easier.
So don’t rush through the basics.
Master them.
Because strong Java developers are built on strong fundamentals.
Reference
(https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html)
(https://www.w3schools.com/java/java_data_types_non-prim.asp)
Top comments (0)