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

Java Programming Basics for BCA Students

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)
39 views8 pages

Java Programming Basics for BCA Students

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

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";


[Link](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();

[Link](myObj.x);

Java Class Methods


Create a method named myMethod() in Main:

public class Main {

static void myMethod() {

[Link]("Hello World!");

OR

public class Main {

static void myMethod() {

[Link]("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() {

[Link]("Static methods can be called without creating objects");

// Public method

public void myPublicMethod() {

[Link]("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

[Link](); // 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

[Link]("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

[Link]();

// Display the value of the brand attribute (from the Vehicle class) and the value of the modelName from the
Car class

[Link]([Link] + " " + [Link]);

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

[Link] = 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() {

[Link]("The animal makes a sound");

class Pig extends Animal {

public void animalSound() {

[Link]("The pig says: wee wee");

}
}

class Dog extends Animal {

public void animalSound() {

[Link]("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

[Link]();

[Link]();

[Link]();

Common questions

Powered by AI

Java ensures modularity and reusability through its object-oriented nature, allowing the creation of classes that serve as templates for objects. Encapsulation hides object details, allowing module independence and easy updates. Inheritance promotes code reuse by sharing logic across classes. Polymorphism enables generic code to operate on objects of multiple types. These capabilities reduce duplication, ease updates, and accelerate development by enabling the reuse of standardized solutions and enhancing maintainability .

Java supports inheritance by allowing a class (subclass) to inherit fields and methods from another class (superclass) using the 'extends' keyword. This promotes code reuse and a hierarchical class organization. For example, if Car extends Vehicle, Car inherits the attributes and methods of Vehicle, allowing an instance of Car to access or override these attributes and functionalities, thus enabling polymorphism and a clear class structure .

Polymorphism in Java is implemented through method overriding and inheritance, allowing objects to take on many forms. This is achieved when a superclass reference is used to refer to a subclass object. An example is using a superclass Animal with a method animalSound(), which is overridden in subclasses Pig and Dog. When the method is called on respective subclass objects, specific subclass methods are executed, demonstrating polymorphism. For instance, if Animal myAnimal = new Dog(); myAnimal.animalSound() is called, it executes the Dog class's animalSound() method .

Java arrays are fixed in size and can store elements of a single data type, providing straightforward data storage with indexed access, suitable for static and simple datasets where size is known in advance. In contrast, collections like ArrayList offer dynamic resizing and more flexible data manipulation. Arrays are optimal for performance-critical tasks due to minimal overhead, whereas collections are better for dynamically-changing datasets .

Declaring variables as private in Java is crucial for encapsulation, a fundamental OOP principle that restricts direct access to an object's data. This ensures that sensitive data is hidden from outside classes, preventing unauthorized access and modification. Encapsulation is further achieved by providing public getter and setter methods to access and update private variables securely, allowing controlled access .

Primitive data types in Java are basic types like byte, int, float, etc., and store simple values directly in memory. Non-primitive data types or reference types, like String and Arrays, refer to objects and store references. Primitive types offer faster access due to direct storage, while non-primitives allow complex data structures. Implications include choices affecting performance and memory management; for instance, objects can lead to overhead but provide more functionality .

Information hiding is violated if class variables are declared public, exposing direct access and allowing potential misuse of sensitive data. For protection, use private access for class variables, complemented by public getter and setter methods to manage access responsibly. For example, a class Person with a public String name field allows any object to modify name, violating encapsulation. Properly protected, this would be private String name with public getName() and setName() methods ensuring controlled access .

Static methods in Java can be called without creating an instance of the class to which they belong, as they are associated with the class itself. For instance, to call a static method myStaticMethod(), one can directly use MyClass.myStaticMethod(). In contrast, public methods require an object for invocation. For example, to call a public method myPublicMethod(), one must first create an object of the class with MyClass obj = new MyClass(); and then call the method on this object with obj.myPublicMethod().

Java's "write once, run anywhere" philosophy is enabled by its use of the Java Virtual Machine (JVM). Compiled Java code is transformed into bytecode, which the JVM can execute. Since the JVM is available on all platforms that support Java, the same bytecode can run on any such platform without modification .

Using encapsulation in Java provides several advantages: it increases modularity as the internal workings of a class are hidden, fosters security by protecting sensitive data from unauthorized access, and simplifies maintenance and modification. It allows class implementations to change without affecting other parts of the program if the public interface remains consistent, enabling easy error localization .

You might also like