Skip to main content

JAVA unit 2 DATA TYPES AND VARIABLES

 Data Types

A data type defines the kind of data a variable can store, the operations that can be performed on it, and the amount of memory allocated.

Java Primitive Data Types

Data Type

Size

Range (Approx.)

Example

Byte

1 byte

-128 to 127

byte a = 100;

short

2 bytes

-32,768 to 32,767

short s = 1000;

Int

4 bytes

-2,147,483,648 to 2,147,483,647

int num = 50000;

Long

8 bytes

-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

long l = 100000L;

float

4 bytes

~6–7 decimal digits

float f = 5.75f;

double

8 bytes

~15 decimal digits

double d = 19.99;

Char

2 bytes

Single Unicode character

char c = 'A';

boolean

1 bit*

true or false

boolean flag = true;

*Boolean size is JVM-dependent (commonly 1 byte in memory, but logically 1 bit).

 

Java Non-Primitive Data Types

Type

Description

Example

String

Sequence of characters

String name = "Dinesh";

Array

Collection of elements

int[] arr = {1, 2, 3};

Class

Blueprint for objects

class Car { }

Object

Instance of a class

Car myCar = new Car();

Interface

Abstract type for methods

interface Shape {}

 Java Variables

A variable in Java is a name given to a memory location where data is stored and manipulated during program execution. Each variable in Java has a data type, which defines:

  • The size of memory to store the value.
  • The range of possible values.
  • The operations that can be performed on it.

Rules for creating a variable name in Java:

  • The name can contain letters (A-Z, a-z), digits (0-9), and the underscore _ or dollar sign $ (although $ is rarely used).
  • The first character must be a letter, underscore, or dollar sign (not a digit).
  • Java is case-sensitive (Name and name are different).
  • Java keywords (like int, class, public) cannot be used as variable names.
  • Variable names should be meaningful and follow camelCase convention for readability.

Syntax:

type variableName = value;

Example:

String name = "Rohit";

int age = 25;

 

 Constants

In Java, a constant is a variable whose value cannot be changed once assigned.
It is declared using the final keyword and is usually written in uppercase letters.
A constant must be given a value when declared, for example:
final double PI = 3.14;.

 

Java Identifiers

In Java, an identifier is the name given to program elements such as classes, methods, variables, and interfaces. They are used to uniquely identify these components.

Rules for defining identifiers in Java:

  • Allowed characters: letters (A-Z, a-z), digits (0-9), underscore _, and dollar sign $.
  • Cannot start with a digit.
  • Cannot contain whitespace.
  • Cannot be a Java keyword (class, public, static, etc.).
  • Java identifiers are case-sensitive.
  • Can be of any length, but extremely long names are discouraged.
  • Should not contain special characters like @, #, -, or spaces.
  • Unicode characters are allowed (e.g., you can technically use non-English characters, but it’s not common).

Valid identifiers:

Name           _name          $price         studentAge

Invalid identifiers:

123name     // starts with a digit

student age // contains a space

class       // keyword

Keywords in Java


Keywords in Java are reserved words that have a predefined meaning in the language. They cannot be used as identifiers (variable names, class names, etc.) because they are part of the Java syntax. There are total 53 keywords in java.

E.g: abstract boolean, break, byte, case, catch, char, class, const,
continue, default, do, double, else, enum, extends, final, finally, float,
for, goto, if, import int, interface, long, new, package, private, protected, public,
return, short, static, switch this try, void while

 

Rules:

  • All keywords are lowercase.
  • Cannot be used for variable, method, or class names.
  • Have a fixed meaning and purpose defined by Java.

 

Comments

Popular posts from this blog

Unit-1 Introduction to C#.NET (Class 12)

  What is .NET Framework? The .NET Framework is a software development platform developed by Microsoft. It provides tools and libraries to build and run Windows applications, web services, and web apps. It gives tools and libraries that make it easier to write programs. It also helps the computer run those programs safely and efficiently. Microsoft started working on .NET Framework in the late 1990s . The first version, .NET Framework 1.0 , was released in 2002 .   The .NET Framework is made up of several important components: 1.       Common Language Runtime (CLR) is the core engine that runs your program. It converts your code into machine code that the computer can understand, manages memory, handles errors, and ensures that your program runs safely. 2.       The Class Library is a large collection of ready-made code that helps you perform common tasks like working with files, databases, graphics, the i...