Pearson Java Notes
Pearson Java Notes
Machine Language-
A computers native language , instructions are in the form of
binary code if you want to give an instruction to computer
you have to enter the instructions as binary code.
Assembly language-
Machine language is very difficult .Assembly language is low
level language. Assembly language uses a short descriptive
word mnemonic.
Assembler is used to translate assembly
language into machine code.
High level language-
Compiler
Compiler translate the entire source code into machine code
and then machine code is finally executed.
Multiprogramming-
Multiprogramming allows multiple programs to run
simultaneously by sharing the same cpu.
Multithreading-
It allows a single program to execute multiple tasks at same
time like we can edit and save data in word at same time.
Java API-
The application program interface (API) also known as library
contains predefined classes and interface for developing java
program.
A java compiler translate a java source code file into java byte
code and .class file is executed by JVM (Java Virtual machine).
Variables-
Variables are for representing data of certain types.
The variable declaration tells the compiler to allocate
appropriate memory space for the variable based on its data
types.
int numbers;
double count,area;
Initialization
int number=1;
k=1;
j=k;
i=j;
Data Types-
1. Object-oriented (Class)
2. Non Object-oriented (Eight primitive data type)
Boolean, byte, long, double, float, int, short, char
These are primitive data types.
Reading of diff. data types from keyboard-
byte bytevalue = input.nextByte();
short shortvalue = input.nextShort();
int intvalue = input.next.Int();
Numeric Operations
Addition (+), Subtraction (-), Multiplication (*), Division (/)
Remainder (%)
Integer Literals
87898978L
long ssn=777_8789_989;
int j = ++i;
if i=1 then after the syntax j=2 and i will be also 2
int j = i++;
if i=1 then after the syntax i=2 but j will remain same (j=1)
if statements-
Enables a program to specify alternative paths of execution.
if(Boolean expression)
{
statements;
}
if(n==true)
{
is equivalent to
if ( n)
{
Operator ^
p1 ^ p2
true ^ true will be false.
true ^ false will be true.(both)
false ^ false will be false.
Switch Statements-
It uses for multiple conditions.
ex. int n = input.nextInt();
switch ( n)
{
case value 1 : ( statement1)
break;
case value2 :(statement2)
break;
default : ( default statement )
break;
value1 and value2 are values of n.
value1 valueN must have same data type.
Trigonometric Methods-
sin(radians) Returns trigonometric sine of angle in radian
cos(radians)- .cos.
tan(radians)-tan
toRadians(degree) - returns angle in radians from degree
toDegree (radians) ..degree..radians
asin(a) returns the angle in radians for the inverse of sine.
acos(a)- cos
atan(a)-.tan
Exponent Method-
exp(x) returns e to the power x
log(x)-returns the natural logarithm of x (ln(x))
log10(x) return the base 10 logarithm of x
sqrt(x) return the square root of x
pow(a,b) return a to the power b
Concatenating Strings -
string s3 = s1.concat(s2);
and a convenient way is
string s3 = s1+s2;
if one of the operands is non string than it is converted to
string;
string s3 = welcome+4;
it will become welcome4;
if(string1==string2)
it doesnt check whether the string1 is equal to string2 it
does only check string1 and string2 refer to the same object.
Comparing of strings
1. equals() method
s1.equals(s2)
2. compareTo() method
s1.compareTo(s2)
if s1 is equal to s2 then it returns 0
equalsIgnoreCase
compareToIgnoreCase
Obtaining Strings
substring(beginIndex,endIndex)
substring(beginIndex)
indexOf(s,from index)
indexOf(s)
Returns the first occurrence of s
lastIndexOf(s,from index)
int count=0;
while(count<100)
{
statement 1;
count++} it runs 100 times.
} }
(a)
when character is stored under string like above , sequence
of string will be changed.
String s1 = "";
char i = 'A'; char j = 'E';
s1 = i+s1;
s1 = j+s1;
System.out.print(s1);
s1 = i+j+s1;
System.out.print(s1);
This will give output 134 (numeric values) not chracters;
Methods
Calling a method
Calling a method executes code in method.
If a method returns a value a call to method is treated as a
value.
If a method returns a void , a call to method must be a
statement.
Modularizing Code
Modularizing makes code easy to maintain
and debug and enables the code to reused.
Overloading Methods
overloading methods enables you to define
the methods with the same name as long as
their signatures are different.
Two methods have same name but have
different parameter list within one class , java
compiler determines which method to use
based on method signature.
foreach loop
for(double e : mylist)
{
System.out.print(e);}
If mylist is an array than it will print whole array.
Copying array
suppose we have 2 array
if list1 = list2 (assign one array reference to
another)
then this statement does not copy array contents
but merely copy the reference value.
3 ways to copy array
- by for loop
- use arraycopy method in system class
- use clone method
Sorting arrays -
selection array
Ragged Arrays-
Rows can have different lengths , this type of array known as
ragged array.
Passing 2 dimensional array to Methods-
Just simple , pass as 1-D array
Object And Classes
Introduction-
Object oriented programming enables you to develop large
scales software and GUI.
Defining classes for Objects-
A class defines the properties and behaviour of an object.
OOP involves programming using objects.
An object has a unique identity , state and behaviour.
The state of an object (also known as properties and
attributes) is represented by data fields with their
current values.
The behaviour of an object (known as its actions) is
defined by methods.
class SimpleCircle{
double radius; (data field)
{return 2*Math.Pi*radius;}
void setradius(double newradius)
{radius = newradius;}
}
java.util.Date class
From date.getTime() method we get Total milliseconds
elapsed since 1 Jan 1970.
And date.toString()
Date(long elapseTime)
Random Class-
Math.random()
obtain a double value between 0.0 to 1.0
java.util.Random
This class generate random number of int , float , double ,
Boolean , long.
when you create a random object , you have to specify a
seed or use the default seed.
A seed is a number used to initialize a random number
generator.
If two random object have same seed then they will produce
same random numbers.
now
p1.distance(p2);
This calculate distances b/w p1 and p2
p1.toString(); return x and y of p1
p1.getX(); returns x of p1
If you want all the instance of a class to share data use static
variables known as class variables.
Static variables store values for the variables in a common
memory location.
Because of this common location , if one object changes the
value of static variable all object of same class are affected.
All the methods in Math class are static and main method
is static too.
Instance variables can accessed via reference variable.
Static variable and static methods can be accessed from
reference variable or from their class name.
Ex-
If our class name is TestCircleWithStaticMembers
then
TestCircleWithStaticMembers . numberOfObjects;
Visibility Modifiers-
Visibility modifiers can be used to specify the visibility of a
class and its members.
If no visibility modifier is used , then by default the classes ,
methods and data fields are accessible by any class in the
same package.
This is known as package private or package access.
Array of Objects-
An array can hold objects as well as primitive data types.
So following statements will create an array of objects.