Bca Java PDF
Bca Java PDF
UNIT 1
Introduction to Java
JAVA was developed by James Gosling at Sun Microsystems Inc in the year 1995, 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.
Java Variables
Variables are containers for storing data values.
String - stores text, such as "Hello". String values are surrounded by double quotes
int - stores integers (whole numbers), without decimals, such as 123 or -123
float - stores floating point numbers, with decimals, such as 19.99 or -19.99
char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes
boolean - stores values with two states: true or false
example:
Primitive data types - includes byte, short, int, long, float, double, boolean and char
Non-primitive data types - such as String, Arrays and Classes (you will learn more about these in a later
chapter)
float 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Bitwise operators
Java Arrays
Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.
Multidimensional Arrays
A multidimensional array is an array of arrays.
Multidimensional arrays are useful when you want to store data as a tabular form, like a table with rows and columns.
Java Classes/Objects
Java is an object-oriented programming language.
Everything in Java is associated with classes and objects, along with its attributes and methods. For example: in real life, a
car is an object. The car has attributes, such as weight and color, and methods, such as drive and brake.
Create an Object
In Java, an object is created from a class. We have already created the class named Main, so now we can use this to
create objects.
To create an object of Main, specify the class name, followed by the object name, and use the keyword new:
Example
int x = 5;
System.out.println(myObj.x);
System.out.println("Hello World!");
OR
System.out.println("Hello World!");
myMethod();
In the example above, we created a static method, which means that it can be accessed without creating an object of the
class, unlike public, which can only be accessed by objects:
Example
// Static method
// Public method
// Main method
In the example below, the Car class (subclass) inherits the attributes and methods from the Vehicle class (superclass):
Example
class Vehicle {
System.out.println("Tuut, tuut!");
// Call the honk() method (from the Vehicle class) on the myCar object
myCar.honk();
// Display the value of the brand attribute (from the Vehicle class) and the value of the modelName from the
Car class
Encapsulation
The meaning of Encapsulation, is to make sure that "sensitive" data is hidden from users. To achieve
this, you must:
Syntax for both is that they start with either get or set, followed by the name of the variable, with the
first letter in upper case:
Example
public class Person {
// Getter
return name;
// Setter
this.name = newName;
Java Polymorphism
Polymorphism means "many forms", and it occurs when we have many classes that are related to each
other by inheritance.
class Animal {
}
}
class Main {
myAnimal.animalSound();
myPig.animalSound();
myDog.animalSound();