Introduction to java
Why Java and why is it important ?
Java is a high level programming language in the in the following tradition of C and C++. You will find yourself comfortable and in familiar with java environment, if you have any experience with C and C++ programming language. you can see various updated features in java. The extra features of the Java programming language are listed below.- C and C++ are platform dependent where as Java is platform independent.
- Java is the object oriented programming language. And C and ++ are both procedure oriented and object oriented programming language.
- Java supports automatic Garbage collection(Garbage Collection is procedure of recovering the runtime unused memory consequently or It is a way to destroy the unused objects.)
- Java is the software based programming language where as C and C++ are nearer to hardware based programming language.
- Java provides more security than the other programming language.
- A development environment, JT provides a large of suit of tools.
- The Java language itself is very simple.
- Java accompanies a library of classes that give regularly utilized utility capacities that most Java programs can't manage without.
Types of Java Programs
- Internet Applets
- Stand-alone Applications
Java Character Sets
Character set is a set of valid characters that a language can recognize. A character represents any letter, digit or any other sign. Java uses the Unicode character set (Unicode is a two-byte character code set that has characters representing almost all characters in almost all human alphabets and writing systems around the world including English, Arabic, Chinese and many more.)Demonstration of the simple java program
/* program HelloWorld*/
class HelloWorld
{
public static void main(String []args)
{
System.out.println(“Hello World!!”);
}
}
Introduction to Classes and objects
As we already described that java is an object oriented programming language above. The basic unit of object-orientation in Java is the class. It is often called as templates or blueprints of objects. It allows a programmer to define all of the properties and methods that internally define the behavior/state of the object.An object is a distinct instance of a given class that is structurally identical to all other instances of that class. A class is an object factory because once the class is created n number of object can be created and use them. A class is the program code you write to create objects. The class describes the data and methods that defines objects behavior.
The world we live in is composed of objects. Everything that we see around us is an example of object. We, ourselves, are examples of objects. All the living and Non-living things like:- Chair, car, pets, bikes, dog, computers, cellphones, house you live in etc can be consider as the example of objects.
Elements Inside Java
Tokens
The tokens are the various Java program elements which are identified by the compiler. they are the smallest elements of program that is meaningful to the compiler. Individual words and punctuation marks. Every unit that makes a sentence.Keywords
The Special words that convey a special meaning to the language compiler. Reserved words for special purpose and must not be used as normal identifier names. There are only 57 keywords in java.Examples of keywords:
abstract default if
private this boolean
do implements protected
throw break double
import public try
Identifiers
The name given to variables, objects, classes, functions, arrays etc.Some of the Identifier forming rules of Java:
- Identifiers can have alphabets, digits and underscore and dollar sign.
- They must not be a keyword or boolean literal or null literal
- They must not begin with a digit.
- They can be of any length.
- Java is case sensitive.
Some valid identifiers:
myName
first_name
lastName
my_address
...
Some invalid identifiers:
first-name
int
phone-no.
......
Identifier Naming Conventions
The names of public methods and instance variables should begin with a lower case letter.
E.g. num1, str_a etc.
For names having multiple words, second and subsequent words beginning character is made
capital. E.g. myName, dateOfBirth etc.
Private and local variables should use lower case letters. E.g. height, width etc.
Constants should be named using all capital letters and underscores. E.g. RATE, PI, CHARGE etc.
Class names and interface names begin with an uppercase letter. E.g. MyClass, Person, Car etc.
Data Types
There are 8 primitive and 2 Non-primitive Date Types in Java language and they are as follows:-Primitive Date Types
- int
- byte
- short
- long
- boolean
- float
- double
- char
- String
- Object
Description of different Data Types:
- Any thing enclosed in single quotes are character(char) data types.
char alphabet= 'A';
- Numbers without fractions are int, short, byte and long data type.
int num = 150;
byte length= 10;
short breadth= 30;
long height= 50;
- Numbers with fractions are float and double data type.
double number= 210.5;
double dis= 0.03;
float res=78.82;
- Anything enclosed in double quotes are a string.
String name= "John"
- Variables that take in either true or false value are a boolean data type.
boolean result= true
Variables
Variables are the name of memory location. There are three types of variable ( Local, Instance and Static) in java. In other word Named storage locations which holds a data value of a particular data type whose value can be manipulated during program run. They must be declared before initialized.The following statement declares a variable name of the data type String
String name;
Some more examples:
double salary, area, volume;
int month, day, year;
long distance, breadth;
Initialization of Variables
boolean result = true;
String last_name= "Adhikari";
int num= 200;Constants
Constant is the kind of variable whose value never changes. The only way to define constant in java is by using final keyword Often in a program you want to give a name to a constant value.For example, you might have a fixed tax rate of 0.13 for electric materials and a tax rate of 0.05 for services.
These are constants because their value is not going to change when the program is executed.
The syntax for declaring constant are:
modifier final datatype variableName= value; // global constant
modifier static final datatype variableName= value; //constant within a class
Operators in Java
Java’s rich set of operators comprises of arithmetic, relational, logical, bitwise , assignment and certain other types of operators.- Arithmetic Operators
Those operators which are required to performs all kinds of Arithmetical operations. Arithmetic operators are used in mathematical expressions in the same way that they are used in algebra. The following table lists the arithmetic operators: Such as:-
- Addition (+)
- Subtraction (-)
- Division (/)
- Multiplication (*)
- Modulus (%)
- Relational Operators
Those operators which area required to perform comparison between operands. Such as:-
- Greater than (>)
- Smaller than (<)
- Greater equals to (>=)
- Smaller equals to (<=)
- equals to (==)
- not equals to (!= or <>)
- The Logical Operator
Those operators required to check the conditions and generates the result in the form of true or false. Such as:-
- AND operator (&&)
- OR operator (||)
- NOT operator (!)
- Assignment Operator
This operator is used to assign the value in variable.
For e.g. String name= "naranshiva";
(Note- '=' is the assignment operator)
Expressions
An expression is composed of one or more operations. The objects of the operation(s) are referred toas operands. The operations are represented by operators. Therefore, operators, constants, and variables are the constituents of expressions.
The expressions in Java can be of any type:
- Arithmetic expressions
- Relational /logical expressions
- Compound expressions
Java Statements
Statements are roughly equivalent to sentences in natural languages. A statement forms a complete unit of execution. The following type of expressions can be made into a statement by terminating the expression with a semicolon(;):Assignment expressions
Any use of ++ or --
Method calls
Object creation expressions
Comments
Post a Comment