Unit 1: Java Fundamentals, Data Types, Operators and Control Statements
By Notes Vandar
1.1 History and Philosophy of Java
History of Java
Java is one of the most influential and widely used programming languages in the world. It was originally developed by James Gosling at Sun Microsystems (now part of Oracle Corporation) and released in 1995. The development of Java began in the early 1990s when Sun Microsystems sought to create a language for embedded systems in consumer electronics.
Key Milestones in Java’s History:
- 1991: The Green Project
- Java started as a part of the Green Project, initiated by James Gosling, Mike Sheridan, and Patrick Naughton. It was aimed at creating a language for programming consumer electronics, such as interactive television.
- Initially, the language was called Oak, named after the oak tree outside Gosling’s office.
- 1995: Public Release of Java 1.0
- Sun Microsystems rebranded Oak to Java, and the first public version of Java (Java 1.0) was released in 1995.
- Java gained popularity because it allowed developers to write programs once and run them anywhere (WORA: “Write Once, Run Anywhere”), making it platform-independent. This was achieved by running Java applications on the Java Virtual Machine (JVM).
- The slogan “write once, run anywhere” highlighted Java’s ability to be run on any platform with a JVM, distinguishing it from languages like C and C++.
- Late 1990s: Rise of Applets and Enterprise Use
- In the late 1990s, Java was used to develop applets (small programs) that could run in web browsers. While this technology eventually became less popular, it initially showcased Java’s potential for web-based applications.
- Java quickly expanded into enterprise-level applications with the introduction of Java 2 Enterprise Edition (J2EE), solidifying its role in large-scale, server-side development.
- 2006: OpenJDK and Oracle Acquisition
- In 2006, Sun Microsystems released much of Java as open source under the OpenJDK (Java Development Kit), making it more accessible to developers.
- In 2010, Oracle Corporation acquired Sun Microsystems, making Oracle the steward of Java. Oracle continues to manage the ongoing development and updates of Java.
- Modern Java: New Features and Versions
- Over time, Java has evolved to incorporate new features and tools. Starting from Java 8 (released in 2014), significant enhancements like lambda expressions, streams, and the Java Time API were introduced, modernizing the language and making it more functional.
- Subsequent versions, including Java 9, 10, 11 (Long-Term Support, LTS), and beyond, introduced features like the Java Module System, Local Variable Type Inference (var), and other performance improvements.
- Java is now widely used in areas like mobile development (via Android), web applications, scientific computing, and large-scale enterprise applications.
Philosophy of Java
The design and philosophy of Java were shaped by several guiding principles, which made it a versatile, secure, and platform-independent language. The key goals of Java’s design philosophy include:
- Simplicity, Familiarity, and Ease of Use
- Java was designed to be easy to use and familiar to programmers coming from languages like C and C++. It eliminates many of the complexities and pitfalls of C++ (e.g., manual memory management, pointers) while retaining object-oriented concepts.
- It aimed to reduce the number of bugs and make it easier to write robust, error-free code.
- Platform Independence
- One of Java’s most famous and defining features is its ability to run on any device or operating system that has a Java Virtual Machine (JVM).
- By compiling Java code into bytecode (intermediate representation) instead of machine code, Java applications can run on any platform that supports the JVM. This achieves the “Write Once, Run Anywhere” (WORA) principle.
- Security
- Security was a major concern in Java’s design, especially since it was intended to run on networked devices and the web.
- Java includes built-in security features such as the sandboxing model for applets (isolating them from the host system), bytecode verification, and access control mechanisms. These features help prevent malicious code from causing damage.
- Object-Oriented
- Java is a fully object-oriented programming language, meaning it uses objects to represent both data and methods.
- The language promotes key object-oriented principles such as inheritance, encapsulation, polymorphism, and abstraction, making it modular, reusable, and easier to maintain.
- Robustness
- Java was designed to be a robust language, minimizing system crashes and memory leaks. It features automatic garbage collection, which automatically deallocates memory that is no longer in use, reducing the chances of memory-related errors.
- Java also features strong type checking at compile time and runtime, reducing bugs related to type mismatches.
- Multithreading
- From the outset, Java was designed with multithreading capabilities, allowing multiple threads to run concurrently within a program. This is especially useful for building high-performance applications like servers and games.
- Java provides built-in support for thread management and synchronization, making it easier to develop applications that can handle multiple tasks simultaneously.
- Distributed and Networked Programming
- Java was created with the ability to handle networked and distributed applications. It provides libraries that simplify network communication, making it easier to build client-server applications.
- Features like Remote Method Invocation (RMI) and JavaBeans allow developers to create distributed, enterprise-level applications.
- High Performance
- Although Java is an interpreted language, modern implementations of the JVM use techniques such as Just-In-Time (JIT) compilation to optimize performance and bring it closer to that of compiled languages like C++.
- Performance improvements in JVMs over the years have significantly reduced Java’s initial reputation for being slower than other compiled languages.
- Dynamic and Extensible
- Java is designed to adapt to evolving environments. Its dynamic nature allows for the addition of new classes, methods, and libraries at runtime.
- Through features like reflection and the ability to load new classes dynamically, Java remains flexible in various runtime environments.
- Community and Ecosystem
- Java has a large and active developer community, which contributes to its rich ecosystem of libraries, frameworks, and tools. This ensures that Java stays relevant and continues to evolve with modern programming needs.
1.2 Object-Oriented Programming (OOP)
Object-Oriented Programming (OOP) is a programming paradigm based on the concept of “objects”, which can contain data (attributes) and code (methods) to manipulate that data. OOP provides a structured approach to program development by organizing software design around these objects. Java is one of the most popular languages that embraces the OOP model, making it central to understanding how Java applications are designed and developed.
Core Concepts of OOP:
- Classes and Objects
- Encapsulation
- Inheritance
- Polymorphism
- Abstraction
1. Classes and Objects
- Class: A class is a blueprint or template for creating objects. It defines the properties (fields/attributes) and behaviors (methods) that the objects created from the class will have.
- Example: A
Car
class may define properties likecolor
,model
, andspeed
, and methods likeaccelerate()
andbrake()
.
- Example: A
- Object: An object is an instance of a class. Once a class is defined, multiple objects can be created from it, each representing a specific entity.
- Example: If
Car
is the class, thenmyCar
oryourCar
could be individual objects representing specific cars.
- Example: If
In Java:
2. Encapsulation
Encapsulation is the concept of restricting access to certain components of an object, so they cannot be accessed directly from outside the class. This is done by marking class variables as private
and providing getter and setter methods for accessing and modifying them.
Encapsulation helps in:
- Data hiding: Keeping the internal workings of an object hidden from the outside.
- Control over data: Allows controlled access and modification of the object’s attributes.
In Java:
3. Inheritance
Inheritance is a mechanism where one class can inherit the properties and behaviors of another class. The class that is inherited from is called the superclass or parent class, and the class that inherits is called the subclass or child class.
Inheritance promotes code reuse and helps in creating hierarchical relationships between classes.
- Single Inheritance: A subclass inherits from only one superclass.
- Multilevel Inheritance: A class can inherit from a subclass, creating multiple levels.
In Java:
4. Polymorphism
Polymorphism allows objects to be treated as instances of their parent class, rather than their actual class. This makes it possible for the same method to behave differently based on the object calling it. There are two types of polymorphism:
- Compile-time Polymorphism (Method Overloading): Multiple methods with the same name but different parameters.
- Run-time Polymorphism (Method Overriding): A subclass can provide a specific implementation of a method that is already defined in its parent class.
- Overloading:
- Overriding:
5. Abstraction
Abstraction refers to the concept of hiding the complex implementation details of a system and only exposing the necessary parts to the user. It focuses on the “what” rather than the “how.”
In Java, abstraction is achieved using:
- Abstract classes: A class that cannot be instantiated and may contain abstract methods (methods without a body).
- Interfaces: A completely abstract class that can only have abstract methods (until Java 8, which introduced default methods).
- Abstract Class:
- Interface:
Advantages of Object-Oriented Programming
- Modularity: Code is organized into objects, making it more manageable and modular.
- Reusability: Classes and objects can be reused in different programs or contexts, promoting code reuse.
- Maintainability: OOP promotes cleaner and more maintainable code by separating concerns.
- Security: Encapsulation allows sensitive data to be hidden from external access, enhancing security.
- Flexibility: Through inheritance and polymorphism, OOP enables flexibility and the ability to extend existing code without modification.
1.3 Java Development Kit (JDK)
The Java Development Kit (JDK) is a crucial tool for developing Java applications. It provides the necessary tools, libraries, and utilities to write, compile, and run Java programs. The JDK is the official development environment for building Java applications and is freely available from Oracle.
Key Components of the JDK
The JDK consists of several important components:
- Java Compiler (
javac
) - Java Runtime Environment (JRE)
- Java Virtual Machine (JVM)
- Development Tools
- Java Libraries
1. Java Compiler (javac
)
The Java compiler (javac
) is responsible for converting the human-readable Java source code into bytecode, which is a platform-independent representation of the program. Bytecode is executed by the Java Virtual Machine (JVM).
- The compiler takes
.java
files and compiles them into.class
files containing bytecode. - Example:
After running this command, the MyProgram.class
file will be generated, which can be executed by the JVM.
2. Java Runtime Environment (JRE)
The Java Runtime Environment (JRE) is a subset of the JDK that provides the libraries and components necessary to run Java programs. While the JDK is used for developing Java programs, the JRE is needed only to run them.
The JRE consists of:
- Java Class Libraries: Pre-built classes that developers can use in their programs, like
java.util
,java.io
, and others. - Java Virtual Machine (JVM): The core component that executes the compiled bytecode.
In essence, the JRE provides the minimum requirements to run a Java application, but it does not include development tools like the compiler.
3. Java Virtual Machine (JVM)
The Java Virtual Machine (JVM) is the heart of Java’s “Write Once, Run Anywhere” philosophy. The JVM is responsible for executing the bytecode generated by the compiler.
- The JVM abstracts the underlying platform (Windows, Mac, Linux, etc.) and allows Java programs to run on any system that has a JVM installed.
- Each operating system has its own JVM implementation, but the bytecode remains the same, making Java platform-independent.
The JVM performs several tasks:
- Class loading: Dynamically loads classes during runtime.
- Bytecode verification: Ensures the bytecode adheres to Java’s security and access rules.
- Execution: Executes the bytecode using either an interpreter or Just-In-Time (JIT) compiler for performance.
4. Development Tools
The JDK includes several development tools that are useful for writing, testing, and debugging Java applications:
javac
: The Java compiler for compiling.java
files into.class
bytecode files.java
: The command used to run Java applications.javadoc
: A tool to generate documentation from Java source code comments.jdb
: A debugger that helps developers find and fix bugs in their Java programs.jar
: A tool for creating and managing JAR (Java ARchive) files, which bundle together compiled Java classes and associated resources.javap
: A disassembler that provides insight into the contents of compiled.class
files.jps
,jstack
,jstat
,jmap
,jconsole
: Various monitoring and profiling tools to analyze the performance of Java applications.
5. Java Libraries (API)
The JDK comes with a rich set of pre-built Java libraries, collectively known as the Java API (Application Programming Interface). These libraries provide the functionality needed for tasks like file handling, networking, data structures, graphical user interface (GUI) development, and much more.
Key packages include:
java.lang
: Fundamental classes likeString
,Math
,System
, and others.java.util
: Utility classes for data structures (likeArrayList
,HashMap
), date and time, etc.java.io
: Input/output classes for reading and writing files and data streams.java.net
: Networking classes for developing networked applications.java.awt
andjavax.swing
: Classes for building graphical user interfaces (GUI).
Installing the JDK
To develop Java programs, the JDK must be installed on your machine. It is available for download from the Oracle website and can be installed on different operating systems, such as Windows, macOS, and Linux.
- After installation, you need to set up the environment variables (
JAVA_HOME
andPATH
) to ensure that Java commands can be run from the command line.
Example (Windows):
This ensures that commands like javac
and java
can be executed from any directory in the terminal.
Workflow of Java Development with the JDK
- Write the Code: Write Java source code using any text editor or an Integrated Development Environment (IDE) like Eclipse or IntelliJ IDEA.
- Compile the Code: Use the
javac
command to compile the source code into bytecode.This generates a
.class
file with bytecode. - Run the Program: Use the
java
command to run the compiled program.This will execute the
main()
method of the class, and the JVM will interpret or compile the bytecode into machine code. - Debug and Optimize: Use debugging tools like
jdb
to step through the code and resolve any errors. Profiling tools likejconsole
can be used to monitor performance.
JDK vs JRE vs JVM
- JVM: Executes bytecode and abstracts the underlying operating system.
- JRE: Contains the JVM and the class libraries required to run Java applications.
- JDK: Includes the JRE and additional tools (compiler, debugger, etc.) for developing Java applications.
1.4 A First Simple Java Program
Let’s go through the process of writing, compiling, and running a simple Java program. Java programs typically follow a structure where they are organized into classes, and every Java program must contain a main
method, which serves as the entry point for the application.
Here’s how you can write a basic Java program that prints a message to the console.
Steps to Create Your First Java Program
Step 1: Write the Code
The following is a simple Java program that prints “Hello, World!” to the console.
Explanation:
public class MyFirstProgram
:- This defines a class named
MyFirstProgram
. Every Java application contains at least one class. - The name of the class should match the filename, so this file should be saved as
MyFirstProgram.java
.
- This defines a class named
public static void main(String[] args)
:- This is the main method. It’s the entry point of any Java program. When you run the program, the JVM looks for the
main
method to start the execution. String[] args
: This parameter is used to accept command-line arguments if needed.
- This is the main method. It’s the entry point of any Java program. When you run the program, the JVM looks for the
System.out.println("Hello, World!");
:- This line prints the text “Hello, World!” to the console.
System.out
is Java’s standard output stream, andprintln
is used to print text followed by a new line.
- This line prints the text “Hello, World!” to the console.
Step 2: Compile the Code
- Save the code in a file named
MyFirstProgram.java
. - Open a terminal or command prompt and navigate to the directory where the file is saved.
- Use the following command to compile the Java file:
This will generate a file called
MyFirstProgram.class
, which contains the compiled bytecode.
Step 3: Run the Program
Once the file is compiled, you can run it using the Java Virtual Machine (JVM) with the java
command.
When you run the above command, you should see the following output:
How It Works
- Compilation: The
javac
compiler takes the Java source code and translates it into bytecode (stored in.class
files). The bytecode is platform-independent, meaning it can be executed on any operating system with a Java Virtual Machine (JVM). - Execution: The
java
command starts the JVM, which interprets the bytecode and runs the program. Themain
method is called, and the statement inside it (System.out.println
) prints “Hello, World!” to the console.
Structure of a Java Program
Here’s a breakdown of a basic Java program structure:
Key Elements:
- Class Declaration (
public class ClassName
): Every Java program is organized into classes. The class name must match the filename. - Main Method (
public static void main(String[] args)
): The method where execution starts. Themain
method must always be written exactly aspublic static void main(String[] args)
. System.out.println
: Used to print messages to the console.
Common Errors
- Missing Main Method: If the
main
method is missing, the JVM will throw an error: - Filename and Class Name Mismatch: If the filename doesn’t match the class name, you’ll get an error during compilation. For example, if your class is named
MyFirstProgram
but the file is saved asProgram.java
, you’ll see: - Semicolon Mistakes: Every statement in Java must end with a semicolon (
;
). If a semicolon is missing, you’ll get a compilation error:
Expanding the Program
Once you’re familiar with the basic structure, you can start experimenting by expanding the program. For example, let’s modify the program to print multiple lines or perform simple operations:
Expected output:
1.5 Packages in Java
Packages in Java are a way of organizing classes and interfaces into namespaces, providing modularity, reusability, and structure to large programs. By grouping related classes, packages help avoid name conflicts and make the code more manageable and maintainable.
What is a Package?
A package in Java is a collection of related classes, interfaces, and sub-packages. Think of it as a directory or folder that logically groups related functionality. For example:
- The
java.util
package contains utility classes like collections, date and time utilities, etc. - The
java.io
package handles input/output operations.
Packages help:
- Prevent name conflicts: Two classes can have the same name but belong to different packages.
- Provide access control: Packages help control the visibility of classes, methods, and fields using access modifiers (
public
,protected
,private
,default
). - Facilitate code reusability: You can import packages and reuse classes across different projects.
Types of Packages
There are two types of packages in Java:
- Built-in Packages: These are predefined packages provided by Java, such as
java.lang
,java.util
, andjava.io
. - User-defined Packages: These are packages created by developers to group and organize their own classes and interfaces.
Using Built-in Packages
Java provides a rich set of built-in packages that you can use directly in your programs by importing them.
Example of using the java.util
package to work with the ArrayList
class:
import java.util.ArrayList;
: This line imports theArrayList
class from thejava.util
package.- Without the import statement, you would have to refer to the class using the full package name, like
java.util.ArrayList
.
Creating User-defined Packages
You can create your own packages in Java to organize your classes. Here’s how:
- Define a Package: Use the
package
keyword at the top of the Java file. - Organize Classes: Classes belonging to the same functionality can be grouped into a package.
Example: Creating and using a user-defined package
- Creating a Package:
- Using the Package:
Explanation:
package mypackage;
: This declares thatMyPackageClass.java
belongs to themypackage
package.- To use this class in another program, you need to import the package using
import mypackage.MyPackageClass;
. - The folder structure should reflect the package name. In this case,
MyPackageClass.java
should be saved in a folder namedmypackage
.
The import
Statement
The import
statement allows you to access classes and interfaces from a package. There are two types of import statements:
- Specific class import: Imports only one class from the package.
- Wildcard import: Imports all classes from the package.
Note: Using wildcard imports (*
) is generally discouraged in large projects as it may lead to unnecessary class loading and potential naming conflicts.
Default Package
If you don’t specify a package using the package
keyword, the class belongs to the default package. The default package doesn’t need to be explicitly imported, but using it is considered poor practice in large projects because it limits reusability and organization.
Package Naming Conventions
- Naming conventions: Package names are usually written in all lowercase to avoid conflicts with class names.
- Domain-based naming: For larger applications, it’s common to use domain names in reverse as package names. For example:
- A company with the domain
example.com
might create a package structure like this: - This prevents naming conflicts with other organizations.
- A company with the domain
Access Control in Packages
Packages, combined with access control modifiers, help manage visibility and encapsulation in Java:
public
: Classes, methods, and fields that are marked aspublic
are accessible from any other class or package.protected
: Accessible within the same package or subclasses.default
(package-private): If no access modifier is specified, the member is accessible only within its own package.private
: Accessible only within the class where it is defined.
Sub-Packages
A package can contain other packages, creating a hierarchy. For example:
Here, utilities
is a sub-package of example
, which is a sub-package of com
. To use a class from a sub-package, the full package name must be specified in the import statement:
Java Package Examples
Some common built-in Java packages include:
java.lang
: Contains fundamental classes likeString
,Math
,Integer
, andThread
. This package is automatically imported in every Java program.java.util
: Provides utility classes such as collections (e.g.,ArrayList
,HashMap
), date/time utilities, and more.java.io
: Contains classes for input and output, such asFile
,InputStream
,OutputStream
, etc.java.nio
: Provides non-blocking I/O operations.java.sql
: Contains classes for database connectivity and operations.javax.swing
: Contains classes for building graphical user interfaces (GUIs).
1.6. Java’s Data Types
Java’s data types are divided into two main categories: Primitive and Non-Primitive types. Below, we explore each type and its uses, focusing on integers, characters, floating-point numbers, strings, arrays, and booleans.
1.6.1 Integers
Integer types are used to store whole numbers (both positive and negative) without decimal points. Java provides four different types of integers based on their size and range:
byte
:- Size: 8 bits
- Range: -128 to 127
- Example:
short
:- Size: 16 bits
- Range: -32,768 to 32,767
- Example:
int
:- Size: 32 bits
- Range: -2^31 to 2^31 – 1 (Approx. -2 billion to +2 billion)
- Example:
long
:- Size: 64 bits
- Range: -2^63 to 2^63 – 1 (Very large range)
- Example:
1.6.2 Characters
In Java, the char
data type is used to store a single character (like a letter or symbol). It uses Unicode encoding, allowing it to represent characters from different languages.
- Size: 16 bits (2 bytes)
- Range: 0 to 65,535 (unsigned)
- Example:
1.6.3 Floating Point Types
Floating-point types are used for representing numbers with decimal points (real numbers). Java has two types of floating-point data types:
float
:- Size: 32 bits (4 bytes)
- Precision: 6-7 decimal digits
- Range: ±3.40282347E+38F
- Example:
double
:- Size: 64 bits (8 bytes)
- Precision: 15 decimal digits
- Range: ±1.79769313486231570E+308
- Example:
For most calculations that involve decimal numbers, double
is the preferred type as it provides better precision.
1.6.4 Strings
Strings in Java are non-primitive data types that represent sequences of characters. They are objects of the String
class and provide many useful methods for manipulating text.
- Example:
Strings are immutable in Java, meaning that once a string is created, it cannot be changed. However, new strings can be created based on modifications to existing ones.
1.6.5 Arrays
An array is a collection of elements of the same data type, stored in contiguous memory locations. Arrays in Java are objects, and they can store both primitive data types and non-primitive types (like objects).
- Declaring an array:
- Accessing array elements:
Arrays in Java are zero-indexed, meaning the first element is accessed at index 0, the second element at index 1, and so on.
1.6.6 The Boolean Type
The boolean
type represents only two possible values: true
or false
. Booleans are typically used for conditions, decision-making, and control flow (such as in if
statements and loops).
- Example:
Unlike other primitive types, boolean
does not have a defined size, but it’s optimized by the Java Virtual Machine (JVM).
1.7 Literals
In Java, literals represent fixed values that are directly used in the program. These values can be assigned to variables or used in expressions. Java supports different types of literals such as integer literals, floating-point literals, boolean literals, character literals, and string literals. This section will focus on special types of literals like hexadecimal, octal, and binary literals, as well as character escape sequences and string literals.
1.7.1 Hex, Octal, and Binary Literals
Java allows you to represent integer literals in hexadecimal, octal, and binary formats, in addition to the usual decimal format.
- Hexadecimal Literals:
- Represented by prefixing the number with
0x
or0X
. - Digits can be from 0-9 and letters from A-F (representing 10-15).
- Example:
- Represented by prefixing the number with
- Octal Literals:
- Represented by prefixing the number with
0
. - Digits can be from 0-7.
- Example:
- Represented by prefixing the number with
- Binary Literals:
- Introduced in Java 7, binary literals are represented by prefixing the number with
0b
or0B
. - Digits can only be 0 or 1.
- Example:
- Introduced in Java 7, binary literals are represented by prefixing the number with
1.7.2 Character Escape Sequences
Character literals in Java can represent special characters that cannot be typed directly in code by using escape sequences. Escape sequences are a combination of a backslash (\
) followed by a character or a series of digits.
Common Escape Sequences:
\n
: New line- Example:
\t
: Tab- Example:
\\
: Backslash- Example:
\'
: Single quote- Example:
\"
: Double quote- Example:
\r
: Carriage return- Example:
\b
: Backspace- Example:
\uXXXX
: Unicode escape (whereXXXX
is a 4-digit hexadecimal number representing a Unicode character)- Example:
1.7.3 String Literals
A string literal is a sequence of characters enclosed in double quotes. In Java, strings are objects of the String
class, which provides various methods to manipulate and process text.
- Example of a simple string literal:
- String concatenation: Strings can be concatenated (joined together) using the
+
operator. - Escape sequences in strings: Special characters can be included in string literals using escape sequences (e.g.,
\n
for a new line or\"
for double quotes).
Multi-line String Literals:
Java doesn’t natively support multi-line string literals (until Java 13 with text blocks), but you can use the newline escape (\n
) for line breaks.
- Example:
With Java 13+, text blocks allow for multi-line strings:
- Example:
1.8 Variables and Constants
In Java, variables and constants are used to store data in memory. Variables can change their values during program execution, while constants retain the same value once they are initialized. Understanding the proper use of variables and constants is crucial for writing clear and maintainable code.
1.8.1 Variables
A variable is a container for storing data values. Each variable in Java has a specific data type that defines what kind of values it can hold (such as integers, floating-point numbers, characters, or strings).
Declaring Variables
Variables must be declared before they can be used. A declaration specifies the data type and variable name.
- Syntax:
- Example:
Initializing Variables
Variables can be initialized at the time of declaration or later in the program.
- Syntax:
- Example:
Types of Variables
In Java, variables can be categorized into the following types:
- Local Variables:
- Declared inside methods, constructors, or blocks.
- Accessible only within the scope where they are defined.
- Example:
- Instance Variables (Non-static fields):
- Declared inside a class but outside any method.
- Each instance (object) of the class has its own copy of the instance variables.
- Example:
- Static Variables (Class variables):
- Declared with the
static
keyword. - Shared among all instances of the class.
- Example:
- Declared with the
1.8.2 Constants
A constant in Java is a variable whose value cannot be changed after it has been initialized. Constants are declared using the final
keyword, and they must be initialized when they are declared.
- Syntax:
By convention, constant names are usually written in uppercase with words separated by underscores.
- Example:
Once a constant is initialized, any attempt to change its value will result in a compilation error.
Key Differences Between Variables and Constants
Feature | Variables | Constants |
---|---|---|
Changeability | Can change value during program execution | Value remains constant once initialized |
Declaration | Declared without final |
Declared with final |
Naming Convention | Use camelCase for naming | Use UPPER_CASE with underscores |
Examples | int age = 25; |
final double PI = 3.14159; |
Example of Variables and Constants in Use
1.10 Type Casting
Type casting in Java is the process of converting a variable from one data type to another. It is essential in programming when you need to ensure that the data types of variables are compatible for operations. Java supports two types of type casting: implicit (automatic) casting and explicit (manual) casting.
1.10.1 Implicit Casting (Widening Conversion)
Implicit casting occurs when a smaller data type is converted to a larger data type automatically by the Java compiler. This is known as widening conversion, where there is no risk of losing data since a larger data type can accommodate the values of a smaller data type.
Examples of Implicit Casting:
- Converting
int
tolong
- Converting
float
todouble
Example:
1.10.2 Explicit Casting (Narrowing Conversion)
Explicit casting is required when converting a larger data type to a smaller data type. This is known as narrowing conversion and must be done manually because it can result in a loss of data.
Examples of Explicit Casting:
- Converting
double
tofloat
- Converting
long
toint
Example:
In the example above, the decimal part is lost during the conversion from double
to int
.
1.10.3 Type Casting in Java
Java supports type casting for various data types, including primitives and objects. Here’s how to perform casting with different data types.
Casting Primitive Types:
- When casting between primitive types, you can use both implicit and explicit casting.
- Implicit casting: Automatically performed.
- Explicit casting: Requires a cast operator.
Example:
Casting Objects:
Casting can also be used when dealing with objects and class hierarchies. When you have a subclass reference pointing to a superclass object, you can cast it back to the subclass type.
Example:
1.10.4 Type Casting with Arrays
You can also cast arrays, but the elements in the array must be compatible types.
Example:
1.10.5 Type Casting Rules
- Widening Casting:
- No explicit cast required.
- Safe as it does not lead to data loss.
- Narrowing Casting:
- Explicit cast required.
- May lead to data loss or precision loss.
- Object Casting:
- Can only cast objects of compatible types.
- Upcasting is safe and implicit.
- Downcasting should be done carefully; use
instanceof
to avoidClassCastException
.