0% found this document useful (0 votes)
1 views8 pages

1.2 Java Basic (1)

Java is a high-level, object-oriented programming language developed by James Gosling in 1995, known for its 'write once, run anywhere' capability. It includes essential components like the Java Virtual Machine (JVM), Java Runtime Environment (JRE), and Java Development Kit (JDK), and supports various data types and operators. The document also covers Java variables, typecasting, and input/output handling, providing examples and explanations for each concept.

Uploaded by

Aditya Chandra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views8 pages

1.2 Java Basic (1)

Java is a high-level, object-oriented programming language developed by James Gosling in 1995, known for its 'write once, run anywhere' capability. It includes essential components like the Java Virtual Machine (JVM), Java Runtime Environment (JRE), and Java Development Kit (JDK), and supports various data types and operators. The document also covers Java variables, typecasting, and input/output handling, providing examples and explanations for each concept.

Uploaded by

Aditya Chandra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

1.

Java: An Overview

What is Java?

●​ Java is a high-level, class-based, object-oriented programming language


designed to have as few implementation dependencies as possible.

●​ It was developed by James Gosling at Sun Microsystems in 1995.

●​ It follows the "write once, run anywhere" (WORA) principle, meaning Java code
can run on all platforms that support Java without needing recompilation.

2. Java Hello World

A simple "Hello World" program in Java:

public class HelloWorld {

public static void main(String[] args) {

System.out.println("Hello, World!");

Breakdown:

●​ public class HelloWorld: Defines a class named HelloWorld.

●​ public static void main(String[] args): Entry point of the program. Every Java
application must have this method.

●​ System.out.println(): Prints text to the console.

3. JVM, JRE, and JDK

Java Virtual Machine (JVM)

●​ JVM is the engine that runs the Java bytecode. It’s platform-dependent.

●​ It interprets compiled Java code (bytecode) and converts it into machine code for
execution.
Java Runtime Environment (JRE)

●​ JRE is a package that provides the necessary libraries and JVM for running Java
applications.

●​ Contains the JVM and core libraries, but doesn't include tools for development
(like the compiler).

Java Development Kit (JDK)

●​ JDK is a superset of JRE that includes tools for developing, compiling, and
debugging Java programs (e.g., javac compiler).

●​ The JDK includes the JRE along with development tools like compilers and
debuggers.

4. Difference Between C, C++, and Java

Feature C C++ Java

Paradigm Procedural Object-Oriented Object-Oriented

Platform-dependent Platform-independen
Platform (compiled to machine Platform-dependent t (bytecode runs on
code) JVM)

Memory Manual + Automatic Garbage


Manual (malloc/free)
Management Constructors/Destructors Collection

Multiple No (supported
No Yes
Inheritance through interfaces)

Pointers Yes Yes No

5. Java Variables

Variables in Java are containers that hold data values.

Types of Variables:

1.​ Local Variable: Declared within a method and accessible only within that
method.
2.​ Instance Variable: Declared in a class but outside any method; belongs to an
instance of the class.

3.​ Static Variable: Declared with the static keyword; belongs to the class rather
than any instance.

Example:

int number = 10; // Local variable

6. Java Data Types

Java data types are divided into two categories:

1.​ Primitive Data Types:

o​ byte, short, int, long (integers)

o​ float, double (floating-point numbers)

o​ char (characters)

o​ boolean (true/false)

Integer Literals
Integers are probably the most commonly used type in the typical program. Any whole number
value is an integer literal. Examples are 1, 2, 3, and 42. These are all decimal values, meaning
they are describing a base 10 number. Two other bases that can be used in integer literals are
octal (base eight) and hexadecimal (base 16). Octal values are denoted in Java by a leading zero.
Normal decimal numbers cannot have a leading zero. Thus, the seemingly valid value 09 will
produce an error from the compiler, since 9 is outside of octal’s 0 to 7 range. A more common
base for numbers used by programmers is hexadecimal, which matches cleanly with modulo 8
word sizes, such as 8, 16, 32, and 64 bits. You signify a hexadecimal constant with a leading
zero-x, (0x or 0X). The range of a hexadecimal digit is 0 to 15, so A through F (or a through f )
are substituted for 10 through 15.

2.​ Non-Primitive Data Types:

o​ String, Arrays, Classes, Interfaces

Example:

java

Copy code

int age = 25;

boolean isStudent = true;

char grade = 'A';

7. Java Operators

Java provides several types of operators to perform different operations.

Categories:

1.​ Arithmetic Operators (+, -, *, /, %)

2.​ Relational Operators (==, !=, >, <, >=, <=)

3.​ Logical Operators (&&, ||, !)

4.​ Assignment Operators (=, +=, -=, etc.)

5.​ Increment/Decrement Operators (++, --)

Example:

int a = 5;

int b = 10;

int sum = a + b; // sum = 15


8. Java Input and Output

Java handles input and output using the java.util.Scanner class and System.out.

Taking Input:

import java.util.Scanner;

public class InputExample {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter your name: ");

String name = scanner.nextLine();

System.out.println("Hello, " + name);

Output:

●​ System.out.print(): Prints without a new line.

●​ System.out.println(): Prints with a new line.

9. Java Expressions & Blocks

Expressions:

●​ An expression is a combination of variables, operators, and values that evaluates


to a single value.

●​ Example: a + b where a = 10 and b = 5.

Blocks:

●​ A block is a group of zero or more statements enclosed in braces {}.

●​ Blocks are typically used to define the body of methods, loops, and conditional
statements.

Example:
{

int x = 10;

System.out.println(x);

10. Java Comments

Java provides three types of comments to document code:

1.​ Single-line Comment: Begins with //

// This is a single-line comment

2.​ Multi-line Comment: Enclosed within /* ... */

/*

This is a multi-line comment.

It spans multiple lines.

*/

3.​ Documentation Comment: Begins with /** ... */ and is used for generating
documentation using Javadoc.

/**

* This is a documentation comment.

* It describes the functionality of methods, classes, etc.

*/
Typecasting in Java

Typecasting is the process of converting one data type to another. It allows you to
change a variable from one type to another explicitly or implicitly. In Java, typecasting is
divided into two types:

a. Implicit (Widening) Typecasting

●​ Occurs automatically when a smaller data type is converted into a larger data
type.

●​ For example, converting an integer to a double.

●​ No data loss occurs since the conversion is from a lower to a higher precision
type.

Example:

int num = 100;

double d = num; // Implicit typecasting: int to double

System.out.println(d); // Output: 100.0

b. Explicit (Narrowing) Typecasting

●​ Requires explicit conversion from a larger data type to a smaller data type.

●​ May cause data loss if the larger type's value doesn't fit within the smaller type's
range.

●​ Done using the cast operator (type).


Example:

double d = 99.99;

int num = (int) d; // Explicit typecasting: double to int

System.out.println(num); // Output: 99

Typecasting is important for converting data types and managing memory usage
efficiently. Java is a strongly-typed language, meaning type conversions must be explicit
in some cases, and type safety is ensured during compilation.

More Theory on Typecasting:

●​ Data Loss: When casting a larger data type (like double) to a smaller type (like
int), the fractional part may be lost.

●​ Type Compatibility: Not all types are compatible for casting. For instance,
casting a String directly to an int is not allowed.

●​ Primitive vs. Reference Typecasting:

o​ Primitive Typecasting: Involves data types like int, float, etc.

o​ Reference Typecasting: Involves converting objects from one class type


to another, usually involving upcasting (from a subclass to a superclass) or
downcasting (from a superclass to a subclass).

Example of Reference Typecasting:

class Animal {}

class Dog extends Animal {}

Animal animal = new Dog(); // Upcasting

Dog dog = (Dog) animal; // Downcasting

You might also like