0% found this document useful (0 votes)
4 views7 pages

Computer Applications - BASIC JAVA CONCEPTS (Part-1)

The document provides an overview of basic Java concepts, including its object-oriented programming features, data types, and the structure of Java programs. It explains key concepts such as classes, objects, encapsulation, inheritance, and polymorphism, along with the syntax for declaring variables and constants. Additionally, it covers operators, expressions, and the process of compiling and executing Java programs.

Uploaded by

b.tpage2010
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)
4 views7 pages

Computer Applications - BASIC JAVA CONCEPTS (Part-1)

The document provides an overview of basic Java concepts, including its object-oriented programming features, data types, and the structure of Java programs. It explains key concepts such as classes, objects, encapsulation, inheritance, and polymorphism, along with the syntax for declaring variables and constants. Additionally, it covers operators, expressions, and the process of compiling and executing Java programs.

Uploaded by

b.tpage2010
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

Computer Applications – Grade X

BASIC JAVA CONCEPTS


Java is
 An Object Oriented Programming (OOP) Language.
 Platform independent Language – It can run across multiple operating systems (OS) like Windows,
Linux, and Macintosh.
 It offers flexibility, scalability and maintainability.
 Developed by James Gosling & Patrick Naughton for Sun Microsystems Inc. in 1991.
 It is now owned by Oracle Corporation.
 It is a Case-Sensitive Language (uppercase is different from lower case).

Concepts of OOP’s
Object: An entity with a specific identity and having specific characteristics and specific behaviour.
Class: It’s a blueprint representing a set of objects that share common characteristics & behaviour.
Data Abstraction: The act of representing essential features without including the background details or
explanations. It hides the internal details.
Encapsulation: The wrapping up of data and functions (that operate on the data) into a single unit (called
class) is known as Encapsulation. Encapsulation is a way to implement data abstraction. It hides the
details of the implementation of the object. It enables access restriction to a class members and
methods by making them public, private or protected.
Modularity: The act of partitioning a program into individual components (modules) is called Modularity.
Inheritance: It is the capability of one class of things to inherit or derive capabilities or properties from
another class.
Polymorphism: It is the ability of the objects to take on many forms.
Polymorphism is achieved through 1. Overloading and 2. Overriding.
Two type of Overloading: 1. Function overloading and 2. Operator overloading

Byte code: It is an intermediary code produced by the Java Compiler after compiling the Java Program.
JVM (Java Virtual Machine): JVM is the interpreter which interprets the byte code into the native
executable code for a particular machine.
Every Java program is first compiled into byte code by the Java Compiler and then interpreted into
machine code by JVM. Hence, Java is both compiled and interpreted based language.

Compiling & Running (Executing) a Java Program from command line:


Command Usage Syntax Example
This command is used to javac <[Link]> javac [Link]
javac
compile a Java Program
This command is used to run java <classname> java Student
java or execute the compiled Java or or
Program java <[Link]> java [Link]
After successful compilation of every Java Program, a class file will be created by the Java Compiler,
which is the intermediary code called Byte Code.
From the Java Class File (Byte Code) only the execution (run/output) of Java Program will be
performed by the Java Virtual Machine (JVM).

P. K. Mathan Raj Page 1 of 11


Computer Applications – Grade X

Types of Java Program:


1. Applications – Standalone programs that can run independently of another program or application.
2. Applets – It is similar to applications but they run inside another application such as web-browser.

Anatomy of a Java Program / Parts or Components of Java Program:


1. Comments – Explanatory texts enclosed within /*…. */ or which follows //.
a. Single line comment -- //
b. Multi-line comment -- /* ………. */
2. Class / Classes – A Java program can have one or more classes. But the name of the class which
contains the main( ) function must be the file name. This class is called as Initial class.
3. Method main( ) – The entry point of every Java application is its main( ) method.
4. Code statements – The methods inside classes contain functionality defined through various
statements.

Java Fundamentals / Building blocks of Java Programming Language / Tokens in Java:


(The smallest individual unit in a program is called as a Token)
1. Keywords 2. Literals / Constants 3. Punctuators
4. Identifiers 5. Operators

Literals: These are data items that have fixed data values.
1. Integer Literals – Whole numbers
a. Decimal Form – starts with a non-zero number (Eg: 10, 434, 2, 54452)
b. Octal Form – starts with a zero (Eg: 04, 056, 076) (Only valid octal numbers)
c. Hexadecimal Form – starts with a 0x (Eg: 0xC, 0x34, 0xFA)
d. Binary Form – starts with 0b or 0B (Eg: 0b111, 0b1010, 0B10110)
2. Floating-point Literals – Numbers with fractional points or decimal points
a. Fractional Form – Eg: 9.34, 0.000045
b. Exponent Form – Eg: 0.36E-3, 0.0934E2
3. Boolean Literals – It takes either true or false value
4. Character Literals – Single character or escape sequence enclosed within single quotation
Eg: ‘a’, ‘3’, ‘\t’
5. String Literals – One or more characters enclosed within double quotation marks
Eg: “Avg”, “abc”, “Everest”, “Emp22”
6. Null Literal – It represents null reference and written as null.

Identifiers: Identifiers are user-defined names given to various program units of Java. Example – Names to
variables, methods, classes, packages, interface, etc.
Identifier Naming Rules:
1. It should start with any alphabet or _ underscore or $ dollar and can be of any length.
2. It must not be a Java keyword or Boolean literal (true, false) or null literal (null).
3. It can contain number but not in the beginning.
4. Java is case sensitive. i.e. upper case is different from lower case.
5. Except _ underscore and $ dollar symbol no other special characters are allowed.

P. K. Mathan Raj Page 2 of 11


Computer Applications – Grade X

Identifier Naming Conventions:


1. Class name should start with capital letter and any subsequent words should also starts with
capital letter.
2. Variable name & method name should start with small letter and any subsequent words should
start with capital letter.
3. Constant names should be given in full capital letter.

Variables: It is a named memory location, which holds a data value of a particular data type.

Constants: A constant value represents a named value that remains fixed throughout an entire program.

Data Types: Data type defines the type of data a variable can hold and the operations that can be
performed on it.
 Primitive data type is used to define and hold a value of basic type in a named variable.
 Reference data types are created using the primitive data types. It is used to store the
memory address of an object.

Data Types

Primitive Types (Intrinsic or Reference Type (Derived or


Fundamental or Basic) User-defined Data type)

Classes
Numeric Non-numeric
Interface

Integer Boolean
Arrays

byte Strings
boolean
short

int

long
Floating-
point
float

double

Character

char

P. K. Mathan Raj Page 3 of 11


Computer Applications – Grade X

 By default all rounded numbers are considered as integer by Java.


Default Values/Literals
 By default all decimal point numbers are considered as double by Java.
 byte → short → int → long → float → double
Widening order of
char → int → long → float → double
Datatype
(Implicit Conversion takes place in this order from left to right)

Data Type Default Value Default size


boolean false Reserves 1 byte, but uses only 1 bit
char null character i.e. '\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
All reference types null -

Type Description Default Size Example Literals Range of values

boolean true or false false 8 bits true, false true, false


byte Integer value 0 8 bits 35, 127, -40 -128 to 127
characters representation of
Unicode ‘a’, ‘\u0041’, ‘\101’,
char \u0000 16 bits ASCII values
character ‘\\’, ‘\’, ‘\n’, ‘β’
0 to 255
short Integer value 0 16 bits 4567, 25, -43 -32,768 to 32,767
(about) -2 billion to +2 billion
(-231 to 231–1)
int Integer value 0 32 bits -2,-1,0,1,2 -2,147,483,648
to
2,147,483,647
(about) -10E18 to +10E18
(-263 to 263–1)
long Integer value 0 64 bits -2L,-1L,0L,1L,2L -9,223,372,036,854,775,808
to
9,223,372,036,854,775,807
Floating point 1.23e100f , -1.23e- -3.4E+38 to +3.4E+38
float 0.0 32 bits
value 100f , .3f ,3.14F upto 7 decimal digits
Floating point 1.23456e300d , - -1.7E+308 to 1.7E+308
double 0.0 64 bits
value 123456e-300d , 1e1d upto 16 decimal digits
\u0000  Unicode character for null

P. K. Mathan Raj Page 4 of 11


Computer Applications – Grade X

Operators: Operators are nothing but symbols which denotes a certain operation. The variables or values
on which the operation is performed is called as Operands.
Eg: a+b is an expression, where a and b are operands and + is an operator.

Types of Operators:
# Types Operators Name Example
1 Arithmetic Operators + Addition a+b
- Subtraction a-b
* Multiplication a*b
/ Division (Quotient) a/b
% Modulation (Remainder) a%b
2 Relational Operators (or) > Greater than A>b
Comparison Operators < Lesser than A<b
>= Greater than or equal to A >= b
<= Lesser than or equal to A <= b
== Equal to A == B
!= Not Equal to A != B
3 Logical Operators && Logical And A > B && A > C
|| Logical Or A > B || A > C
! Logical Not !A
4 Shift Operators >> Signed Right shift A >> 2
<< Signed Left shift B << 4
>>> Unsigned Right shift A >>> 2
5 Bitwise Operators & Bitwise AND 1&1
| Bitwise OR 1|0
^ Bitwise XOR 1^1
~ Bitwise NOT (Complement) ~1
6 Assignment Operator = Assignment operation C = 22
7 Arithmetic Assignment Operators A += B
+= A=A+B
(or) Short hand Operators
-= A=A–B A -= B
*= A=A*B A *= B
/= B=B/C B /= C
%= D=D%C D %= C
Other Short hand Operators &=, |=, ^=  Bitwise Assignment Operators
>>=, <<=, >>>=  Shift Assignment Operators
8 Increment / Decrement Operators A++ (Postfix)
++ Increment
(Prefix – Change-then-use) ++A (Prefix)
(Postfix – Use-then-Change) A-- (Postfix)
-- Decrement
-- A (Prefix)
9 Conditional Operator (or) Ternary
?: For simple if…. else C = 26 > 34 ? 5 : 32;
Operator

P. K. Mathan Raj Page 5 of 11


Computer Applications – Grade X

Operator Precedence & Associativity:


 Operator precedence determines the order in which sub-expressions are evaluated in an
expression.
 Associativity Rules determine the grouping of operands and operators in an expression with more
than one operator of the same precedence.
Priority/Precedence Operator Associativity
1 [] () Left to Right
2 ++ -- ! ~ instanceof Right to Left
3 new (typecasting) Right to Left
4 * / % Left to Right
5 + - Left to Right
6 << >> >>> Left to Right
7 < > <= >= Left to Right
8 == != Left to Right
9 & ^ | Left to Right
10 && || Left to Right
11 ?: Right to Left
12 = += -= *= /= %= ^= Right to Left
13 &= |= <<= >>= >>>= Right to Left
Expressions:
 It is a valid combination of operators, constants and variables. i.e., legal combination of Java
Tokens.
 An expression is made up of operands and operators.
Types of Expressions in Java:
# Type Description Example
1 Arithmetic Expressions that use arithmetic operators are called Pure Integer Expression:
Expressions Arithmetic Expressions. int a,b,c,d;
Arithmetic Expressions containing all integer type d = a + b – c * d;
operands are called as Pure Integer Expressions.
Arithmetic Expressions containing only real numbers Real Expression:
are called as Real Expressions. float b, h, area;
Sometimes, a mixed expression can also be formed, Area = 0.5 * b * h;
which is a mixture of real and integer expressions.
Mixed Expression:
(a * [Link](x, 2) + b)
2 Relational Expressions that establish the relation of two or more X > Y
Expressions variables/values using relational operators are A != B
Relational Expressions. C == D
3 Logical Expressions that use logical operators to combine (x > y) && (x > z)
Expressions relational expressions are called Logical Expressions.
4 Boolean The expression that result in either true or false is X > Y
Expressions called Boolean Expression. It is a combination of (Y + Z) >= (X / Z)
constants, variables, logical & relational operators. (a + b) >= c && (c <= d)

P. K. Mathan Raj Page 6 of 11


Computer Applications – Grade X

GENERAL FORMAT or SYNTAX:


Syntax
# Topic Example
Clause enclosed in [ ] is optional clause.
BASIC JAVA CONCEPTS
Variable int a;
1 <datatype> <variablename>;
Declaration double height, weight;
Constant
2 final <datatype> <constantname> = value; final double pi = 3.14;
Declaration
import [Link];
class Demo
{
p s v m (String[] args)
1. Import Scanner class from util. pkg. {
2. Create Scanner class obj with argument as Scanner s = new
3 To get user Input [Link] Scanner([Link]);
3. Use the necessary next___() method for int no;
the type of the data. double ht;
no = [Link]();
ht = [Link]();
}
}
[Link](2, 3);
Using Math Library
4 Math.function_name(argument list) [Link](23.23);
Methods
[Link](25);
Conditional Constructs in Java
if(expression)
{ if (a>b)
statements; } { [Link](“a is greater”); }
5 if statement
else else
{ { [Link](“b is greater”); }
statements; }
if(expression-1) if (avg>=80)
{ statements; } { [Link](“A Grade”); }
else if(expression-2) else if (avg>=70 && avg<80)
{ statements; } { [Link](“B Grade”); }
6 if…else if…. ladder
else if(expression-n) else if (avg>=50 && avg<70)
{ statements; } { [Link](“C Grade”); }
else else
{ statements; } { [Link](“D Grade”); }
switch(expression) {
case <constant-1> : statements; break;
7 switch statement case <constant-2> : statements; break;
case <constant-n> : statements; break;
[default : statements; ] }

P. K. Mathan Raj Page 7 of 11

You might also like