[go: up one dir, main page]

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

Bca Java PDF

This document provides an introduction to the Java programming language including: - A brief history of Java and how it was developed by Sun Microsystems and later acquired by Oracle. - An overview of Java variables and data types including strings, integers, floats, characters, and booleans. - Explanations of Java arrays, classes/objects, and how to create objects from classes. - Descriptions of Java concepts like inheritance, encapsulation, polymorphism, and examples demonstrating static vs public methods.

Uploaded by

chandrek
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)
79 views8 pages

Bca Java PDF

This document provides an introduction to the Java programming language including: - A brief history of Java and how it was developed by Sun Microsystems and later acquired by Oracle. - An overview of Java variables and data types including strings, integers, floats, characters, and booleans. - Explanations of Java arrays, classes/objects, and how to create objects from classes. - Descriptions of Java concepts like inheritance, encapsulation, polymorphism, and examples demonstrating static vs public methods.

Uploaded by

chandrek
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

Programming in Java(BCA 5th)(Soban singh jeena university)

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.

In Java, there are different types of variables, for example:

 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:

String name = "John";


System.out.println(name);

Java Data Types

Data types are divided into two groups:

 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)

byte 1 byte Stores whole numbers from -128 to 127

short 2 bytes Stores whole numbers from -32,768 to 32,767

int 4 bytes Stores whole numbers from -2,147,483,648 to 2,147,483,647


long 8 bytes Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

float 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits

Java divides the operators into the following groups:

 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.

int arr[] = new int[5];

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.

A Class is like an object constructor, or a "blueprint" for creating objects.

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

Create an object called "myObj" and print the value of x:


public class Main {

int x = 5;

public static void main(String[] args) {

Main myObj = new Main();

System.out.println(myObj.x);

Java Class Methods


Create a method named myMethod() in Main:

public class Main {

static void myMethod() {

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

OR

public class Main {

static void myMethod() {

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

public static void main(String[] args) {

myMethod();

Static vs. Public


You will often see Java programs that have either static or public attributes and methods.

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

An example to demonstrate the differences between static and public methods:

public class Main {

// Static method

static void myStaticMethod() {

System.out.println("Static methods can be called without creating objects");

// Public method

public void myPublicMethod() {

System.out.println("Public methods must be called by creating objects");

// Main method

public static void main(String[] args) {

myStaticMethod(); // Call the static method

// myPublicMethod(); This would compile an error

Main myObj = new Main(); // Create an object of Main

myObj.myPublicMethod(); // Call the public method on the object

Java Inheritance (Subclass and Superclass)


In Java, it is possible to inherit attributes and methods from one class to another. We group the "inheritance concept" into
two categories:

 subclass (child) - the class that inherits from another class


 superclass (parent) - the class being inherited from

To inherit from a class, use the extends keyword.

In the example below, the Car class (subclass) inherits the attributes and methods from the Vehicle class (superclass):
Example

class Vehicle {

protected String brand = "Ford"; // Vehicle attribute

public void honk() { // Vehicle method

System.out.println("Tuut, tuut!");

class Car extends Vehicle {

private String modelName = "Mustang"; // Car attribute

public static void main(String[] args) {

// Create a myCar object

Car myCar = new Car();

// 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

System.out.println(myCar.brand + " " + myCar.modelName);

Encapsulation
The meaning of Encapsulation, is to make sure that "sensitive" data is hidden from users. To achieve
this, you must:

 declare class variables/attributes as private


 provide public get and set methods to access and update the value of a private variable

Get and Set


You learned from the previous chapter that private variables can only be accessed within the same
class (an outside class has no access to it). However, it is possible to access them if we provide
public get and set methods.
The get method returns the variable value, and the set method sets the value.

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 {

private String name; // private = restricted access

// Getter

public String getName() {

return name;

// Setter

public void setName(String newName) {

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 {

public void animalSound() {

System.out.println("The animal makes a sound");

class Pig extends Animal {

public void animalSound() {

System.out.println("The pig says: wee wee");

}
}

class Dog extends Animal {

public void animalSound() {

System.out.println("The dog says: bow wow");

class Main {

public static void main(String[] args) {

Animal myAnimal = new Animal(); // Create a Animal object

Animal myPig = new Pig(); // Create a Pig object

Animal myDog = new Dog(); // Create a Dog object

myAnimal.animalSound();

myPig.animalSound();

myDog.animalSound();

You might also like