0% found this document useful (0 votes)
7 views19 pages

Bca IV Sem Unit I (Java)

The document provides an overview of Java programming, highlighting its features such as platform independence, object-oriented structure, and security. It discusses the history of Java, its applications, and the importance of the Java Development Kit (JDK) and Java Runtime Environment (JRE). Additionally, it covers Java's data types and the significance of garbage collection and classpath in Java programming.

Uploaded by

Sahil Chouhan
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)
7 views19 pages

Bca IV Sem Unit I (Java)

The document provides an overview of Java programming, highlighting its features such as platform independence, object-oriented structure, and security. It discusses the history of Java, its applications, and the importance of the Java Development Kit (JDK) and Java Runtime Environment (JRE). Additionally, it covers Java's data types and the significance of garbage collection and classpath in Java programming.

Uploaded by

Sahil Chouhan
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/ 19

RENAISSANCE UNIVERSITY, INDORE

BCA IV SEM
Subject: Java Programming
Unit I:

Introduction to Java

Java is a class-based, object-oriented programming language that is designed to have as few


implementation dependencies as possible. It is intended to let
application developers write once, and run anywhere (WORA),
meaning that compiled Java code can run on all platforms that
support Java without the need for recompilation. Java was first
released in 1995 and is widely used for developing applications for
desktop, web, and mobile devices. Java is known for its simplicity,
robustness, and security features, making it a popular choice for
enterprise-level applications.

JAVA was developed by James Gosling at Sun Microsystems Inc in the year 1995 and later
acquired by Oracle Corporation. It is a simple programming language. Java makes writing,
compiling, and debugging programming easy. It helps to create reusable code and modular
programs. Java is a class-based, object-oriented programming language and is designed to have as
few implementation dependencies as possible. A general-purpose programming language made for
developers to write once run anywhere that is compiled Java code can run on all platforms that
support Java. Java applications are compiled to byte code that can run on any Java Virtual Machine.
The syntax of Java is similar to c/c++.

History: Java’s history is very interesting. It is a programming language created in 1991. James
Gosling, Mike Sheridan, and Patrick Naughton, a team of Sun engineers known as the Green team
initiated the Java language in 1991. Sun Microsystems released its first public implementation in
1996 as Java 1.0. It provides no-cost -run-times on popular platforms. Java1.0 compiler was re-
written in Java by Arthur Van Hoff to strictly comply with its specifications. With the arrival of
Java 2, new versions had multiple configurations built for different types of platforms.

Java is used for:

• Mobile applications (specially Android apps)


• Desktop applications
• Web applications
• Web servers and application servers
• Games
• Database connection
• And much, much more!!

1|Page
Study Notes (For Private Circular Only)
RENAISSANCE UNIVERSITY, INDORE
BCA IV SEM
Subject: Java Programming
Unit I:

Why Java is so popular?

• Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.)
• It is one of the most popular programming languages in the world
• It has a large demand in the current job market
• It is easy to learn and simple to use
• It is open-source and free
• It is secure, fast and powerful
• It has huge community support (tens of millions of developers)
• Java is an object-oriented language which gives a clear structure to programs and allows
code to be reused, lowering development costs
• As Java is close to C++ and C#, it makes it easy for programmers to switch to Java or vice
versa.

Why Java Programming Language is Called JAVA?

Initially, the name oak was given to the language. However, the team decided that a new name
should be given to the language, and words like, DNA, revolutionary, Jolt, Dynamic, Silk, etc. were
suggested. All these names were fun to say and easy to spell out. But what was missing was the
essence of the language in the suggested names, which the team wanted to have. As per James
Gosling, Java, and Silk were two of the most popular options, and since Java had a unique name,
most people preferred it.

Java is also a name of an island in Indonesia where coffee (named Java Coffee) was produced. The
name Java was chosen by James Gosling because he was having coffee near his office. Readers
should note that Java is not an acronym. It is just a name.

2|Page
Study Notes (For Private Circular Only)
RENAISSANCE UNIVERSITY, INDORE
BCA IV SEM
Subject: Java Programming
Unit I:

Primary/Main Features of Java:

1. Platform Independent: Compiler converts source code to bytecode and then the JVM executes
the bytecode generated by the compiler. This bytecode can run on any platform be it Windows,
Linux, or macOS which means if we compile a program on Windows, then we can run it on Linux
and vice versa. Each operating system has a different JVM, but the output produced by all the OS is
the same after the execution of the bytecode. That is why we call java a platform-independent
language.

2. Object-Oriented Programming Language: Organizing the program in the terms of a collection


of objects is a way of object-oriented programming, each of which represents an instance of the
class.

The four main concepts of Object-Oriented programming are:

❖ Abstraction
❖ Encapsulation
❖ Inheritance
❖ Polymorphism

3. Simple: Java is one of the simple languages as it does not have complex features like pointers,
operator overloading, multiple inheritances, and Explicit memory allocation.

4. Robust: Java language is robust which means reliable. It is developed in such a way that it puts
a lot of effort into checking errors as early as possible, that is why the java compiler is able to detect
even those errors that are not easy to detect by another programming language. The main features of
java that make it robust are garbage collection, Exception Handling, and memory allocation.

5. Secure: In java, we don’t have pointers, so we cannot access out-of-bound arrays i.e it shows
ArrayIndexOutOfBound Exception if we try to do so. That’s why several security flaws like stack
corruption or buffer overflow are impossible to exploit in Java. Also, java programs run in an
environment that is independent of the os(operating system) environment which makes java
programs more secure.

6. Distributed: We can create distributed applications using the java programming language.
Remote Method Invocation and Enterprise Java Beans are used for creating distributed applications
in java. The java programs can be easily distributed on one or more systems that are connected to
each other through an internet connection.

3|Page
Study Notes (For Private Circular Only)
RENAISSANCE UNIVERSITY, INDORE
BCA IV SEM
Subject: Java Programming
Unit I:

7. Multithreading: Java supports multithreading. It is a Java feature that allows concurrent


execution of two or more parts of a program for maximum utilization of the CPU.

8. Portable: As we know, java code written on one machine can be run on another machine. The
platform-independent feature of java in which its platform-independent bytecode can be taken to
any platform for execution makes java portable.

9. High Performance: Java architecture is defined in such a way that it reduces overhead during
the runtime and at some times java uses Just In Time (JIT) compiler where the compiler compiles
code on-demand basics where it only compiles those methods that are called making applications to
execute faster.

10. Dynamic flexibility: Java being completely object-oriented gives us the flexibility to add
classes, new methods to existing classes, and even create new classes through sub-classes. Java
even supports functions written in other languages such as C, C++ which are referred to as native
methods.

11. Sandbox Execution: Java programs run in a separate space that allows user to execute their
applications without affecting the underlying system with help of a bytecode verifier. Bytecode
verifier also provides additional security as its role is to check the code for any violation of access.

12. Write Once Run Anywhere: As discussed above java application generates a ‘.class’ file that
corresponds to our applications(program) but contains code in binary format. It provides ease t
architecture-neutral ease as bytecode is not dependent on any machine architecture. It is the primary
reason java is used in the enterprising IT industry globally worldwide.

13. Power of compilation and interpretation: Most languages are designed with the purpose of
either they are compiled language or they are interpreted language. But java integrates arising
enormous power as Java compiler compiles the source code to bytecode and JVM executes this
bytecode to machine OS-dependent executable code.

4|Page
Study Notes (For Private Circular Only)
RENAISSANCE UNIVERSITY, INDORE
BCA IV SEM
Subject: Java Programming
Unit I:

JAVA Terminology

Before moving further, there are some Java terms that we need to understand. So let's have a brief
understanding of the Java terminology.

Java Development Kit (JDK)


As the name formally states, Java Development Kit is a full-time kit that has a compiler, Java
Runtime Environment (JRE), Debuggers, and Java documents included in it. For further execution
in java, we need to have JDK installed on our computers to further lead to the creation, compilation,
and running of the java program.

Here, as we use JDK, we need an environment to run the programs. We use JRE Java Runtime
Environment, which provides the least requirements to execute the Java program. It provides the
JVM, Core classes, and supporting files.

5|Page
Study Notes (For Private Circular Only)
RENAISSANCE UNIVERSITY, INDORE
BCA IV SEM
Subject: Java Programming
Unit I:

Java Run-time Environment (JRE)


JDK includes JRE, which, in turn, after installation, allows the Java program to run. But we still
can't compile it. It has a browser, the applet supports, and a few plugins included in it. So, to run a
java program on your respective computers, you need JRE.

JRE is made up of multiple elements altogether, and they are:

1. Java virtual machine (JVM)


2. Java class libraries
3. Java class loader

When our software tends to execute a particular program, it requires some environment to run in.
Usually, it's any operating system, for example, Unix, Linux, Microsoft Windows, or macOS. Here
JRE acts as a translator and facilitator between the java program and the operating system.

Java Virtual Machine (JVM)


This generally is referred to as JVM and contains three phases that we have to follow. It is a very
important part not only of JDK but also JRE as it is inbuilt in both of the places. When you run a
program using the JRE and JDK, it also goes to the JVM as it is required to run the java program
and interprets the program. The phases are as follows:

Compile the Code: The Java Development Kit(JDK) provides us with the JAVAC compiler to get
through this step. We basically require the JDK to convert our source code into a specific format
(compiled code) that can be easily interpreted by the Java Runtime Environment (JRE).

Run the Code: JVM runs the bytecode provided by the compiler. Since Java is a platform-
independent language, the compiled code produced by the JAVAC compiler is converted to
machine code using platform specific JVMs. Different platforms have different JVMs. JVMs
convert the bytecode into platform specific machine code.

6|Page
Study Notes (For Private Circular Only)
RENAISSANCE UNIVERSITY, INDORE
BCA IV SEM
Subject: Java Programming
Unit I:

Let’s now see how this bytecode is developed, making it easy for us to execute our programs.

Bytecode in the Development Process


The source code provided to the JAVAC compiler that is present in JDK gets compiled into a
bytecode that the JVM can execute. It gets saved as a .class file by the compiler itself. We can also
view this bytecode using a disassembler like javap.

Garbage Collector
Within Java, the programmers cannot delete the objects. Hence, to delete or recollect a memory,
JVM has a Garbage Collector. These Garbage Collectors can recollect those objects which have not
been referenced. Using this Java makes the life of a programmer very convenient by managing the
memory. Despite this, programmers should be aware of what they are writing in their codes or if
they are using objects that have already been used for longer. This is because this collector can’t
restore the memory of objects that are referenced. Garbage collection here is an automatic process.

Class Path
Classpath is a particular file path where both the Java runtime and Java compiler look for .class files
to appear. Although by default, the Java Development Kit provides us with quite a few libraries.
Here you can also include an external set of libraries, but they should be added to the classpath
beforehand.

7|Page
Study Notes (For Private Circular Only)
RENAISSANCE UNIVERSITY, INDORE
BCA IV SEM
Subject: Java Programming
Unit I:

Simple Program in Java

import java.io.*;

public class Main {


public static void main(String[] args) {
System.out.println("Welcome to JAVA");
}
}

Output:
Welcome to JAVA

Explanation:

1. import java.io.- Import is a keyword that includes the class we use in the code. This is used to
import all the classes of the io package. Java's io package includes a set of input and output streams
used to read or write files and input or output.

2. public class-The data and methods that need to be used in the program are included in the class.
Methods define the behaviour of the class.

3. public static-Static methods do not require objects to get called. They can be executed using the
dot operator with the class name.

4. void- Does not return any value. It's an empty void.

5. main()- Name of the method. Entry point method where JVM runs the program.

6. (String[ ] args)- These are used as command-line arguments and are passed as strings.

7. System.out.println- This command prints the contents provided within the print statement.

8|Page
Study Notes (For Private Circular Only)
RENAISSANCE UNIVERSITY, INDORE
BCA IV SEM
Subject: Java Programming
Unit I:

Points to Remember

• Java can be a class-based object-oriented programming language designed to minimize


implementation dependencies as much as possible.
• Java is a high-level, easily understandable language with syntax similar to C/C++.
• Java is platform-independent, and this makes it better than any other programming
language. Java implements the write once run anywhere principle.
• Java is a portable, simple, secure, and robust programming language well-suited for large
codebases.
• The JDK is a development environment for building applications, applets, and components
using the Java programming language.
• The JDK includes tools useful for developing and testing programs written in the Java
programming language and running on the Java platform.
• JRE is made up of multiple elements altogether, and they are: Java virtual machine (JVM),
Java class libraries, and Java class loader.
• Both JDK and JRE require JVM, which is specifically responsible for converting bytecode
into machine-specific code.
• JVM is platform-dependent; it carries out a variety of tasks, such as memory management.
In addition, JVM may execute programs that have been converted to Java bytecode from
other programming languages.
• Garbage collector in Java is used to recollect those objects which cannot be referenced
anymore.
• Classpath is a particular file path where both the Java runtime and Java compiler look for
.class files to appear.
• Java is used in Multithreaded, distributed, dynamic, and high-performance applications.

9|Page
Study Notes (For Private Circular Only)
RENAISSANCE UNIVERSITY, INDORE
BCA IV SEM
Subject: Java Programming
Unit I:

Data Types in Java

Data types specify the different sizes and values that can be stored in the variable. There are two
types of data types in Java:

Primitive data types: The primitive data types include boolean, char, byte, short, int, long, float and
double.
Non-primitive data types: The non-primitive data types include Classes, Interfaces, and Arrays.

In Java language, primitive data types are the building blocks of data manipulation. These are the
most basic data types available in Java language.

There are 8 types of primitive data types:

1. boolean data type


2. byte data type
3. char data type
4. short data type
5. int data type
6. long data type
7. float data type
8. double data type

10 | P a g e
Study Notes (For Private Circular Only)
RENAISSANCE UNIVERSITY, INDORE
BCA IV SEM
Subject: Java Programming
Unit I:

Data Type Default Value Default size


boolean FALSE 1 bit
char '\u0000' 2 byte
byte 0 1 byte
short 0 2 byte
int 0 4 byte
long 0L 8 byte
float 0.0f 4 byte
double 0.0d 8 byte

11 | P a g e
Study Notes (For Private Circular Only)
RENAISSANCE UNIVERSITY, INDORE
BCA IV SEM
Subject: Java Programming
Unit I:

Java Type Casting

In Java, type casting is a method or process that converts a data type into another data type in both
ways manually and automatically. The automatic conversion is done by the compiler and manual
conversion performed by the programmer.

Type casting is when you assign a value of one primitive data type to another type.

In Java, there are two types of casting:

Widening Casting (automatically) - converting a smaller type to a larger type size


byte -> short -> char -> int -> long -> float -> double

Narrowing Casting (manually) - converting a larger type to a smaller size type


double -> float -> long -> int -> char -> short -> byte

Widening Casting:
Widening casting is done automatically when passing a smaller size type to a larger size type:

Example:

public class Main {


public static void main(String[] args) {
int myInt = 9;
double myDouble = myInt; // Automatic casting: int to double

System.out.println(myInt); // Outputs 9
System.out.println(myDouble); // Outputs 9.0
}
}

Narrowing Casting:
Narrowing casting must be done manually by placing the type in parentheses in front of the value:

Example
public class Main {
12 | P a g e
Study Notes (For Private Circular Only)
RENAISSANCE UNIVERSITY, INDORE
BCA IV SEM
Subject: Java Programming
Unit I:

public static void main(String[] args) {


double myDouble = 9.78d;
int myInt = (int) myDouble; // Manual casting: double to int

System.out.println(myDouble); // Outputs 9.78


System.out.println(myInt); // Outputs 9
}
}

Operators in Java

Java provides many types of operators which can be used according to the need. They are classified
based on the functionality they provide. In this article, we will learn about Java Operators and learn
all their types.

What are the Java Operators?


Operators in Java are the symbols used for performing specific operations in Java. Operators make
tasks like addition, multiplication, etc which look easy although the implementation of these tasks is
quite complex.

Types of Operators in Java


There are multiple types of operators in Java all are mentioned below:

1. Arithmetic Operators
2. Unary Operators
3. Assignment Operator
4. Relational Operators
5. Logical Operators
6. Ternary Operator
7. Bitwise Operators
8. Shift Operators
9. instance of operator

1. Arithmetic Operators
They are used to perform simple arithmetic operations on primitive data types.

13 | P a g e
Study Notes (For Private Circular Only)
RENAISSANCE UNIVERSITY, INDORE
BCA IV SEM
Subject: Java Programming
Unit I:

Operator Name Description Example


+ Addition Adds together two values x+y
- Subtraction Subtracts one value from another x-y
* Multiplication Multiplies two values x*y
/ Division Divides one value by another x/y
% Modulus Returns the division remainder x%y
++ Increment Increases the value of a variable by 1 ++x
-- Decrement Decreases the value of a variable by 1 --x

2. Unary Operators
Unary operators need only one operand. They are used to increment, decrement, or negate a value.

– : Unary minus, used for negating the values.


+ : Unary plus indicates the positive value (numbers are positive without this, however). It performs
an automatic conversion to int when the type of its operand is the byte, char, or short. This is called
unary numeric promotion.
++ : Increment operator, used for incrementing the value by 1. There are two varieties of increment
operators.
Post-Increment: Value is first used for computing the result and then incremented.
Pre-Increment: Value is incremented first, and then the result is computed.
– – : Decrement operator, used for decrementing the value by 1. There are two varieties of
decrement operators.
Post-decrement: Value is first used for computing the result and then decremented.
Pre-Decrement: The value is decremented first, and then the result is computed.
! : Logical not operator, used for inverting a boolean value.

3 Java Assignment Operators


Assignment operators are used to assign values to variables.

In the example below, we use the assignment operator (=) to assign the value 10 to a variable called
x:

Example
int x = 10;

A list of all assignment operators:


Operator Example Same As
= x=5 x=5
14 | P a g e
Study Notes (For Private Circular Only)
RENAISSANCE UNIVERSITY, INDORE
BCA IV SEM
Subject: Java Programming
Unit I:

+= x += 3 x=x+3
-= x -= 3 x=x-3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
%= x %= 3 x=x%3
&= x &= 3 x=x&3
|= x |= 3 x=x|3
^= x ^= 3 x=x^3
>>= x >>= 3 x = x >> 3
<<= x <<= 3 x = x << 3

4. Java Comparison/Relational Operators


Comparison operators are used to compare two values (or variables). The return value of a
comparison is either true or false. These values are known as Boolean values.

Example
int x = 5;
int y = 3;
System.out.println(x > y); // returns true, because 5 is higher than 3

Operator Name Example


== Equal to x == y
!= Not equal x != y
> Greater than x>y
< Less than x<y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y

5. Java Logical Operators


We can also test for true or false values with logical operators.
Logical operators are used to determine the logic between variables or values:

Operator Name Description Example


&& Logical and Returns true if both statements are true x < 5 && x < 10
|| Logical or Returns true if one of the statements is true x < 5 || x < 4
! Logical not Reverse the result, returns false if the result is true !(x<5 && x<10)

6. Ternary operator

15 | P a g e
Study Notes (For Private Circular Only)
RENAISSANCE UNIVERSITY, INDORE
BCA IV SEM
Subject: Java Programming
Unit I:

The ternary operator is a shorthand version of the if-else statement. It has three operands and hence
the name Ternary.

The general format is:


condition ? if true : if false
The above statement means that if the condition evaluates to true, then execute the statements after
the ‘?’ else execute the statements after the ‘:’.

7. Bitwise Operators
These operators are used to perform the manipulation of individual bits of a number. They can be
used with any of the integer types. They are used when performing update and query operations of
the Binary indexed trees.

& Bitwise AND operator: returns bit by bit AND of input values.
| Bitwise OR operator: returns bit by bit OR of input values.
^ Bitwise XOR operator: returns bit-by-bit XOR of input values.
~ Bitwise Complement Operator: This is a unary operator which returns the one’s complement
representation of the input value, i.e., with all bits inverted.

// Java Program to implement


// bitwise operators
import java.io.*;

// Driver class
class Operator {
// main function
public static void main(String[] args)
{
// Bitwise operators
int d = 0b1010;
int e = 0b1100;
System.out.println("d & e: " + (d & e));
System.out.println("d | e: " + (d | e));
System.out.println("d ^ e: " + (d ^ e));
System.out.println("~d: " + (~d));
System.out.println("d << 2: " + (d << 2));
System.out.println("e >> 1: " + (e >> 1));
System.out.println("e >>> 1: " + (e >>> 1));
16 | P a g e
Study Notes (For Private Circular Only)
RENAISSANCE UNIVERSITY, INDORE
BCA IV SEM
Subject: Java Programming
Unit I:

}
}

8. Shift Operators
These operators are used to shift the bits of a number left or right, thereby multiplying or dividing
the number by two, respectively. They can be used when we have to multiply or divide a number by
two. General format-

number shift_op number_of_places_to_shift;

<< Left shift operator: shifts the bits of the number to the left and fills 0 on voids left as a result.
Similar effect as multiplying the number with some power of two.
>> Signed Right shift operator: shifts the bits of the number to the right and fills 0 on voids left as
a result. The leftmost bit depends on the sign of the initial number. Similar effect to dividing the
number with some power of two.
>>> Unsigned Right shift operator: shifts the bits of the number to the right and fills 0 on voids left
as a result. The leftmost bit is set to 0.

int a = 10;

// using left shift


System.out.println("a<<1 : " + (a << 1));

// using right shift


System.out.println("a>>1 : " + (a >> 1));

9. instanceof operator
The instance of the operator is used for type checking. It can be used to test if an object is an
instance of a class, a subclass, or an interface. General format-

object instance of class/subclass/interface

17 | P a g e
Study Notes (For Private Circular Only)
RENAISSANCE UNIVERSITY, INDORE
BCA IV SEM
Subject: Java Programming
Unit I:

Java Control Statements

Java compiler executes the code from top to bottom. The statements in the code are executed
according to the order in which they appear. However, Java provides statements that can be used to
control the flow of Java code. Such statements are called control flow statements. It is one of the
fundamental features of Java, which provides a smooth flow of program.

Java provides three types of control flow statements.

1. Decision Making statements


a. if statements
b. switch statement
2. Loop statements
a. do while loop
b. while loop
c. for loop
d. for-each loop
3. Jump statements
a. break statement
b. continue statement

Java for-each loop

Java provides an enhanced for loop to traverse the data structures like array or collection. In the for-
each loop, we don't need to update the loop variable. The syntax to use the for-each loop in java is
given below.

for(data_type var : array_name)


{ //statements
}

Example:
public class Sample {
public static void main(String[] args) {
String[] names = {"Java","C","C++","Python","JavaScript"};
System.out.println("Printing the content of the array:");
for(String name:names) {
System.out.println(name);
}
18 | P a g e
} Study Notes (For Private Circular Only)
}
RENAISSANCE UNIVERSITY, INDORE
BCA IV SEM
Subject: Java Programming
Unit I:

Output:
Printing the content of the array:

Java
C
C++
Python
JavaScript

Java Command Line Arguments

The java command-line argument is an argument i.e. passed at the time of running the java
program.

The arguments passed from the console can be received in the java program and it can be used as an
input.

So, it provides a convenient way to check the behavior of the program for the different values. You
can pass N (1,2,3 and so on) numbers of arguments from the command prompt.

Simple example of command-line argument in java


In this example, we are receiving only one argument and printing it. To run this java program, you
must pass at least one argument from the command prompt.

class CommandLineExample{
public static void main(String args[]){
System.out.println("Your first argument is: "+args[0]);
}
}

19 | P a g e
Study Notes (For Private Circular Only)

You might also like