0% found this document useful (0 votes)
11 views

Java Part1

This document discusses the history and features of the Java programming language. It provides an overview of Java's origins in the early 1990s and describes key Java concepts like object-oriented programming, platform independence, security, and performance. The document also outlines procedures for developing a basic Java program and defining variables, keywords, and comments.

Uploaded by

SANTHOSHKUMAR M
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Java Part1

This document discusses the history and features of the Java programming language. It provides an overview of Java's origins in the early 1990s and describes key Java concepts like object-oriented programming, platform independence, security, and performance. The document also outlines procedures for developing a basic Java program and defining variables, keywords, and comments.

Uploaded by

SANTHOSHKUMAR M
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

PROGRAMMING IN

1|Page
PROGRAMMING IN JAVA – II BCA [GTMC]
HISTORY OF JAVA

• The C language developed in 1972 by Dennis Ritchie had taken a decade to become
the most popular language.
• In 1979, Bjarne Stroustrup developed C++, an enhancement to the C language with
included OOP fundamentals and features.
• A project named “Green” was initiated in December of 1990, whose aim was to
create a programming tool that could render obsolete the C and C++ programming
languages.
• Finally in the year of 1991 the Green Team was created a new Programming
language named “OAK”.
• After some time they found that there is already a programming language with the
name “OAK”.
• So, the green team had a meeting to choose a new name. After so many
discussions they want to have a coffee. They went to a Coffee Shop which is just
outside of the Gosling’s office and there they have decided name as “JAVA”.

2|Page
PROGRAMMING IN JAVA – II BCA [GTMC]
FEATURES OF JAVA

1. Simple, easy and familiar:


Java is easy to learn and familiar because java syntax is just like c++.
It is simple because:
a) it does not use header files.
b) eliminated the use of pointer and operator overloading.
2. Platform Independent:
Write once, run anywhere (WORA).

3.Object-Oriented:
Java is Object oriented throughout language- that mean no coding outside of
class definitions, including main().
4. Robust:
Robust means inbuilt capabilities to handle errors/exceptions.
Java is robust because of following:
1. Built-in Exception handling.
2. Strong type checking i.e. all data must be declared an explicit type.
3. Local variables must be initialized.
4. First checks the reliability of the code before Execution etc.
3|Page
PROGRAMMING IN JAVA – II BCA [GTMC]
5. Secure:
Java is secure because it provides:
1. access restrictions with the help of access modifiers (public, private etc).
2. byte codes verification – checks classes after loading.
Class loader – confines objects to unique namespaces.
Security manager – determines what resources a class can access such as reading and
writing to the local disk.
6. Compiled and interpreted:
Java code is translated into byte code after compilation and the byte code is interpreted
by JVM (Java Virtual Machine). This two steps process allows for extensive code checking
and also increase security.
7. Portable:
Means able to be easily carried or moved. Write once, run anywhere (WORA) feature
makes it portable.
8. Architecture-Neutral:
Java code is translated into byte code after compilation which is independent of any
computer architecture, it needs only JVM (Java Virtual Machine) to execute.
9. High performance:
JVM can execute byte codes (highly optimized) very fast with the help of Just in time (JIT)
compilation technique.
10. Re-usability of code:
Java provides the code reusability With the Help of Inheritance.
11. Multithreading:
Java provides multitasking facility with the help of lightweight processes called threads.
12. Dynamic:
Java have the capability of linking dynamic new classes, methods and objects.

4|Page
PROGRAMMING IN JAVA – II BCA [GTMC]
Java Identifiers

Identifiers in Java are a sequence of characters to identify something in a program.


They are names given to a class, variable, package, method, or interface and allow the
programmer to refer to the specific item from any place in the program.

Keywords in Java
Keywords in Java are reserved words that represent predefined actions, internal
processes etc. Because of this, keywords cannot be used as names of variables, functions,
objects etc.
Some of the keywords in the Java are given as follows −

abstract assert boolean break

byte case catch char

class const continue default

do double else enum

extends final finally float

for goto if implements

import instanceof int interface

long native new package

private protected public return

short static strictfp super

switch synchronized this throw

throws transient try void

volatile while

5|Page
PROGRAMMING IN JAVA – II BCA [GTMC]
Procedure to develop Java program

First Java program

class FirstProgram
{
public static void main(String args[])
{
System.out.println(“Welcome Java”);
}
}

Save: FirstProgram.java
Compile: C:\> javac FirstProgram.java
Run: C:\> java FirstProgram

Output: Welcome Java

6|Page
PROGRAMMING IN JAVA – II BCA [GTMC]
Explanation:
• class is a keyword in Java and everything is written under the class.
• public is an access modifier which provides the accessibility of main method to all.
• static is a keyword and main methods are always declared as static. There is no
need to create an object to invoke the static method.
• void shows the main methods that do not return any kind of value.
• String args[] is used for command line argument.
• System.out.println() is used for printing the statement.

Execution Process of Java Program

The following three steps are used to create and execute a java program.

• Create a source code (.java file).


• Compile the source code using javac command.
• Run or execute .class file uisng java command.

7|Page
PROGRAMMING IN JAVA – II BCA [GTMC]
Variable in Java
• Variable is an identifier which holds data or another one variable is an identifier
whose value can be changed at the execution time of program.
• Variable is an identifier which can be used to identify input data in a program.

Syntax

Variable_name = value;

Rules to declare a Variable

• Every variable name should start with either alphabets or underscore ( _ ) or


dollar ( $ ) symbol.
• No space are allowed in the variable declarations.
• Except underscore ( _ ) no special symbol are allowed in the middle of
variable declaration
• Variable name always should exist in the left hand side of assignment
operators.
• Maximum length of variable is 64 characters.
• No keywords should access variable name.

8|Page
PROGRAMMING IN JAVA – II BCA [GTMC]
Variable declarations

In which sufficient memory will be allocated and holds default values.

Syntax

Datatype variable_name;
byte b1;

Variable initialization

It is the process of storing user defined values at the time of allocation of memory space.

Variable assignment

Value is assigned to a variable if that is already declared or initialized.

Syntax

Variable_Name = value
int a = 100;

9|Page
PROGRAMMING IN JAVA – II BCA [GTMC]
Syntax

int a= 100;
int b;
b = 25; // ------> direct assigned variable
b = a; // ------> assigned value in term of variable
b = a+15; // ------> assigned value as term of expression

There are three types of variables in Java:

o local variable
o instance variable
o static variable

1) Local Variable

A variable declared inside the body of the method is called local variable. You can use this
variable only within that method and the other methods in the class aren't even aware
that the variable exists.

A local variable cannot be defined with "static" keyword.

2) Instance Variable

A variable declared inside the class but outside the body of the method, is called an
instance variable. It is not declared as static.

It is called an instance variable because its value is instance-specific and is not shared
among instances.

10 | P a g e
PROGRAMMING IN JAVA – II BCA [GTMC]
3) Static variable

A variable that is declared as static is called a static variable. It cannot be local. You can
create a single copy of the static variable and share it among all the instances of the class.
Memory allocation for static variables happens only once when the class is loaded in the
memory.

11 | P a g e
PROGRAMMING IN JAVA – II BCA [GTMC]
Java Comments
Comments in Java allow you to describe the code. This makes the Java code easy to read.
Whatever you add inside a comment never gets compiled. Comments in a Java program
can be mentioned in the following two ways:

• Single-line Comment
• Multi-line Comments

Single-line comments
Use // i.e., two forward slashes for a single-line comment in Java. Whatever we mention
after the slashes in a single line, will not get compiled.

Multi-line comments
For multi-line comments in Java, use /* */. Whatever we mention under /* and */, will
not get compiled.

12 | P a g e
PROGRAMMING IN JAVA – II BCA [GTMC]
Java Type Casting
The Primitive datatypes in Java are the following: byte, short, int, long, float, double,
boolean, and char. Type Casting is to convert one data type to another. There are two
types of Type Casting in Java:

• Automatic Type Casting


• Manual Type Casting

Automatic/ Widening Type Casting in Java


The Widening type casting is handled by Java on its own i.e., it occurs when you want to
convert a smaller type to a larger type.

Manual/ Narrowing Type Casting in Java


The narrowing type casting is handled by Java on its own i.e., it occurs when you want to
convert a larger type to a smaller type.

13 | P a g e
PROGRAMMING IN JAVA – II BCA [GTMC]
Java Data Types
Java programming language has a rich set of data types. The data type is a category of
data stored in variables. In java, data types are classified into two types and they are as
follows.

• Primitive Data Types


• Non-primitive Data Types

Primitive Data Types

• The primitive data types are built-in data types and they specify the type of value
stored in a variable and the memory size.
• The primitive data types do not have any additional methods.

14 | P a g e
PROGRAMMING IN JAVA – II BCA [GTMC]
The following table provides more description of each primitive data type.

Data Meaning Memory Range Default


type size Value

byte Whole 1 byte -128 to +127 0


numbers

short Whole 2 bytes -32768 to +32767 0


numbers

int Whole 4 bytes -2,147,483,648 to +2,147,483,647 0


numbers

long Whole 8 bytes -9,223,372,036,854,775,808 to 0L


numbers +9,223,372,036,854,775,807

float Fractional 4 bytes - 0.0f


numbers

double Fractional 8 bytes - 0.0d


numbers

char Single 2 bytes 0 to 65535 \u0000


character

boolean unsigned 1 bit 0 or 1 0 (false)


char

15 | P a g e
PROGRAMMING IN JAVA – II BCA [GTMC]
Non-primitive Data Types

• In java, non-primitive data types are the reference data types or user-created data
types.

• All non-primitive data types are implemented using object concepts.

• Every variable of the non-primitive data type is an object.

• The non-primitive data types may use additional methods to perform certain
operations.

• The default value of non-primitive data type variable is null.

16 | P a g e
PROGRAMMING IN JAVA – II BCA [GTMC]

You might also like