Translate

Sunday, 12 November 2017

java if statement



5.1 If statement


if statement

An if statement contains a Boolean expression and block of statements enclosed within braces.


if(conditional expression)

//statement or compound statement;

else 

//optional

//statement or compound statement; 

//optional


If the Boolean expression is true then statement block is executed otherwise (if false) program directly goes to next statement without executing Statement block.


if....else

If statement block with else statement is known as as if...else statement. Else portion is non-compulsory.


if ( condition_one ) 

{

//statements

}

else if ( condition_two )

{

//statements

}

else

{

//statements

}


If the condition is true, then compiler will execute the if block of statements, if false then else block of statements will be executed.


nested if...else

when a series of decisions are involved, we may have to use more than one if...else statement in nested form as follows:


if(test condition1)

{

if(test condition2)

{

//statement1;

}

else

{

//statement2;


}

}

else

{

//statement3;

}

//statement x;


Java program operator



4.1 Operators


Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

- Arithmetic Operators

- Relational Operators

- Bitwise Operators

- Logical Operators

- Assignment Operators

- Misc Operators


Arithmetic Operators:

Arithmetic operators are used in mathematical expressions in the same way that they are used in algebra.

arithmetic operators:

+ Additive operator (also used for String concatenation)

- Subtraction operator

* Multiplication operator

/ Division operator

% Remainder operator


Relational Operators:

There are following relational operators supported by Java language

> Greater than

< Less than

== Equal to

!= Not equal to

>= Greater than or equal to

<= Less than or equal to


Bitwise Operators:

Java defines several bitwise operators, which can be applied to the integer types, long, int, short, char, and byte.

Bitwise operator works on bits and performs bit-by-bit operation. 


~ Unary bitwise complement

<< Signed left shift

>> Signed right shift

>>> Unsigned right shift & Bitwise AND

^ Bitwise exclusive OR

| Bitwise inclusive OR


Logical Operators:

The following table lists the logical operators:

&& Conditional-AND

|| Conditional-OR

?: Ternary (shorthand for if-then-else statement)


Assignment Operators:

There are following assignment operators supported by Java language:

= Simple assignment operator

+= Add AND assignment operator

-= Subtract AND assignment operator

*= Multiply AND assignment operator

/= Divide AND assignment operator

%= Modulus AND assignment operator

<<= Left shift AND assignment operator.

>>= Right shift AND assignment operator

&= Bitwise AND assignment operator.

^= bitwise exclusive OR and assignment operator.

|= bitwise inclusive OR and assignment operator.


Increment and Decrement Operators

Increment and decrement operators are used to add or subtract 1 from the current value of oprand.

++ increment

-- decrement


Increment and Decrement operators can be prefix or postfix. 

In the prefix style the value of oprand is changed before the result of expression and in the postfix style the variable is modified after result.


For eg.

a = 9;

b = a++ + 5;

/* a=10 b=14 */


a = 9;

b = ++a + 5;

/* a=10 b=15 */


Miscellaneous Operators

There are few other operators supported by Java Language.

Conditional Operator ( ? : )

Conditional operator is also known as the ternary operator.

The operator is written as:

variable x = (expression) ? value if true : value if false


Instance of Operator:

This operator is used only for object reference variables.

instanceof oper

ator is wriiten as:

( Object reference variable ) instanceof (class/interface type)


Java data types



3.2 Data type


Every variable in Java has a data type. Data types specify the size and type of values that can be stored.

Data types in Java divided primarily in two tyeps: 

Primitive(intrinsic) and Non-primitive.


Primitive types contains Integer, Floating points, Characters, Booleans And Non-primitive types contains Classes, Interface and Arrays.


Integer:This group includes byte, short, int and long, which are whole signed numbers.

Floating-point Numbers: This group includes float and double, which represent number with fraction precision.

Characters: This group includes char, which represents character set like letters and number

Boolean: This group includes Boolean, which is special type of representation of true or false value.


Some data types with their range and size:

byte: -128 to 127 (1 byte)


short: -32,768 to +32,767 (2 bytes)


int: -2,147,483,648 to +2,147,483,647 (4 bytes)


float: 3.4e-038 to 1.7e+0.38 (4 bytes)


double: 3.4e-038 to 1.7e+308 (8 bytes)


char : holds only a single character(2 bytes)


boolean : can take only

 true or false (1 bytes)


Java program variables



3.1 Variables


A variable provides us with named storage that our programs can manipulate. 

Each variable in Java has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.


You must declare all variables before they can be used. 

The basic form of a variable declaration is shown here:

data_type variable = value;


Here data type is one of Java's datatypes and variable is the name of the variable. To declare more than one variable of the specified type, you can use a comma-separated list.


Following are valid examples of variable declaration and initialization in Java:

int a, b, c;

// Declares three ints, a, b, and c.


int a = 10, b = 10;

// Example of initialization


double pi = 3.14159;

// declares and assigns a value of PI.


char a = 'a';

// the char variable a iis initialized with value 'a'


Constant: During the execution of program, value of variable may change. A constant represents permanent data that never changes. 


If you want use some value likes p=3.14159; no need to type every time instead you can simply define constant for p, following is the syntax for declaring constant.

Static final datatype ConstantName = value;


Example: stat

ic final float PI=3.14159; 


Java program features



1.2 Features


1. Simple

Java is easy to learn and its syntax is quite simple and easy to understand.


2. Object-Oriented

In java everything is Object which has some data and behaviour. Java can be easily extended as it is based on Object Model.


3. Platform independent

Unlike other programming languages such as C, C++ etc which are compiled into platform specific machines. Java is guaranteed to be write-once, run-anywhere language.


On compilation Java program is compiled into bytecode. This bytecode is platform independent and can be run on any machine, plus this bytecode format also provide security. Any machine with Java Runtime Environment can run Java Programs.


4. Secured

When it comes to security, Java is always the first choice. With java secure features it enable us to develop virus free, temper free system. Java program always runs in Java runtime environment with almost null interaction with system OS, hence it is more secure.


5. Robust

Java makes an effort to eliminate error prone codes by emphasizing mainly on compile time error checking and runtime checking. But the main areas which Java improved were Memory Management and mishandled Exceptions by introducing automatic Garbage Collector and Exception Handling.


6. Architecture neutral

Compiler generates bytecodes, which have nothing to do with a particular computer architecture, hence a Java program is easy to intrepret on any machine.


7. Portable

Java Bytecode can be carried to any platform. No implementation dependent features. Everything related to storage is predefined, example: size of primitive data types 


8. High Performance

Java is an interpreted language, so it will never be as fast as a compiled language like C or C++. But, Java enables high performance with the use of just-in-time compiler.


9. Multithreaded

Java multithreading feature makes it possible to write program that can do many tasks simultaneously. Benefit of multithreading is that it utilizes same memory and other resources to execute multiple threads at the same time, like While typing, grammatical errors are checked along.


10. Distributed

We can create distributed applications in java. RMI and EJB are used for creating distributed applications. We may access files by calling the methods from any machine on the internet.


11. Interpreted

An interpreter is needed in order to run Java programs. The programs are compiled into Java Virtual Machine code called bytecode. The bytecode is machine independent and is able to run on any machine that has a Java interpreter. With Java, the program need only be compiled once, and the bytecode generated by the Java c

ompiler can run on any platform. 


introduction Java




1.1 Introduction

Java is a simple and yet powerful object oriented programming language and it is in many respects similar to C++.

Java is created by James Gosling from Sun Microsystems (Sun) in 1991. The first publicly available version of Java (Java 1.0) was released in 1995.

Java is defined by a specification and consists of a programming language, a compiler, core libraries and a runtime machine(Java virtual machine).
The Java runtime allows software developers to write program code in other languages than the Java programming language which still runs on the Java virtual machine.
The Java platform is usually associated with the Java virtual machine and the Java core libraries.

Java virtual machine
The Java virtual machine (JVM) is a software implementation of a computer that executes programs like a real machine.

Java Runtime Environment vs. Java Development Kit
A Java distribution typically comes in two flavors, the Java Runtime Environment (JRE) and the Java Development Kit (JDK).
The JRE consists of the JVM and the Java class libraries. Those contain the necessary functionality to start Java programs.
The JDK additionally contains the development tools necessary to create Java programs. The JDK therefore consists of a Java compiler, the Java virtual machine and the Java class libraries.

Uses of JAVA
Java is also used as the programming language for many different software programs, games, and add-ons.

Some examples of the more widely used programs written in Java or that use Java include the Android apps, Big Data Technologies, Adobe Creative suite, Eclipse, Lotus Notes, Minecraft, OpenOffice, Runescape, and Vuze.


Featured post

check box

<!DOCTYPE html> <html> <head>   <title>Check Box</title> </head> <body>   <input ty...