Monday, 18 January 2016

JAVA STRINGS

Java String provides a lot of concepts that can be performed on a string such as compare, concat, equals, split, length, replace, compareTo, intern, substring etc.
In java, string is basically an object that represents sequence of char values.

An array of characters works same as java string. For example:
char[] ch={'j','a','v','a'};
String s= new String(ch);
is same as 
String s="java";

WHAT IS STRING IN JAVA?
Generally, string is a sequence of characters. But in java, string is an object that represents a sequence of characters. String class is used to create string object.

HOW TO CREATE STRING OBJECT ?
There are two ways to create String object:
  1. By string literal
  2. By new keyword
1.String Literal
Java String literal is created by using double quotes. For Example:
String s="java";
Each time you create a string literal, the JVM checks the string constant pool first. If the string already exists in the pool, a reference to the pooled instance is returned. If string doesn't exist in the pool, a new string instance is created and placed in the pool. For example:
String s1="java";
String s2="java";//will not create new instance

WHY JAVA USES CONCEPT OF STRING LITERAL?
To make Java more memory efficient (because no new objects are created if it exists already in string constant pool).

2.New Keyword
String s=new String("Java");//creates two objects and one reference variable  
In such case, JVM will create a new string object in normal(non pool) heap memory and the literal "Java" will be placed in the string constant pool. The variable s will refer to the object in heap(non pool).




Friday, 15 January 2016

JAVA KEYWORDS

In the Java programming language, a keyword is one of 50 reserved words that have a predefined meaning in the language; because of this, programmers cannot use keywords as names for variablesmethodsclasses, or as any other identifier.
abstractassertbooleanbreakbyte
casecatchcharclassconst
continuedefaultdodoubleelse
enum
extends
finalfinallyfloat
forgotoifimplementsimport
instanceofintinterfacelongnative
newpackageprivateprotectedpublic
returnshortstaticstrictfpsuper
switchsynchronizedthisthrowthrows
transienttryvoidvolatilewhile

Thursday, 14 January 2016

OPERATORS IN JAVA

Java provides a rich set of operators to manipulate the variables. Java operators can be divided into following groups:
  • Arithmetic operators
  • Relation operators
  • Logical operators
  • Bitwise operators
  • Assignment operators
  • Misc operators
1. Arithmetic Operators:
Arithmetic operators are used in mathematical expression in the same way that are used in algebra.
OperatorDescription
+adds two operands
-subtract second operands from first
*multiply two operand
/divide numerator by denominator
%remainder of division
++Increment operator increases integer value by one
--Decrement operator decreases integer value by one
2.Relation Operators:
The following table shows all relation operators supported by Java.
OperatorDescription
==Check if two operand are equal
!=Check if two operand are not equal.
>Check if operand on the left is greater than operand on the right
<Check operand on the left is smaller than right operand
>=check left operand is greater than or equal to right operand
<=Check if operand on left is smaller than or equal to right operand
3.Logical Operators:
The following table shows all relation operators supported by Java.
OperatorDescription
==Check if two operand are equal
!=Check if two operand are not equal.
>Check if operand on the left is greater than operand on the right
<Check operand on the left is smaller than right operand
>=check left operand is greater than or equal to right operand
<=Check if operand on left is smaller than or equal to right operand
4.Bitwise Operators:
Java defines several bitwise operators that can be applied to the integer types long, int, short, char and byte
operatordescription
&Bitwise AND
|Bitwise OR
^Bitwise exclusive OR
<<left shift
>>right shift
5.Assignment Operators:
Assignment operator supported by Java are as follows
operatordescriptionexample
=assigns values from right side operands to left side operanda=b
+=adds right operand to the left operand and assign the result to lefta+=b is same as a=a+b
-=subtracts right operand from the left operand and assign the result to left operanda-=b is same as a=a-b
*=mutiply left operand with the right operand and assign the result to left operanda*=b is same as a=a*b
/=divides left operand with the right operand and assign the result to left operanda/=b is same as a=a/b
%=calculate modulus using two operands and assign the result to left operanda%=b is same as a=a%b
6.Misc Operators:
There are few other operator supported by java language.
1.Conditional Operator:It is also known as ternary operator and used to evaluate Boolean expression. It is written as exp1 ? exp2 : exp3
If exp1Condition is true? Then value exp2 : Otherwise value exp3

2.InstanceOf Operator:This operator is used for object reference variables. The operator checks whether the object is of particular type (class type or interface type).It is written as 
(Object of reference variable ) instanceOf (class/interface type)

Wednesday, 13 January 2016

VARIABLES IN JAVA

Java Programming language defines three kind of variables.
  1. Instance variables
  2. Static Variables
  3. Local Variables
1.Instance Variables
Instance variables are variables that are declare inside a class but outside any method,constructor or block. Instance variable are also variable of object commonly known as field or property.
example:
class Student 
{
String name;
int age;
}
Here name and age are instance variable of Student class.

2.Static Variables
Static are class variables declared with static keyword. Static variables are initialized only once. Static variables are also used in declaring constant along with final keyword.
example:
class Student 
{
String name;
int age;
static int branchCode=112;
}
Here branchCode is a static variable. Each object of Student class will share branchCode property.

3.Local Variables
Local variables are declared in method constructor or blocks. Local variables are initialized when method or constructor block start and will be destroyed once its end. Local variable reside in stack. Access modifiers are not used for local variable.
float getDiscount(int mrp)
{
float discount;
discount=mrp*(10/100);
}
Here discount is a local variable.

Tuesday, 12 January 2016

IDENTIFIERS IN JAVA

All Java components require names. Name used for classes, methods, interfaces and variables are called Identifier. Identifier must follow some rules. Here are the rules:
  • All identifiers must start with either a letter( a to z or A to Z ) or currency character($) or an underscore(_).
  • After the first character, an identifier can have any combination of characters.
  • A Java keyword cannot be used as an identifier.
  • Identifiers in Java are case sensitive, off and Off are two different identifiers.
There are two types of identifiers
  • Legal
  • Illegal
The following are legal variable names:
  • MyVariable
  • myvariable
  • MYVARIABLE
  • x
  • i
The following are illegal variable names;
  • My Space //Contains a space
  • 9pins6s // Begins with a digit
  • k+h // The plus sign is not an alphanumeric character testing
  • 61-72-83 // The hyphen is not an alphanumeric character

Monday, 11 January 2016

DATATYPES IN JAVA

Java language has a rich implementation of data types. Data types specify size and the type of values that can be stored in an identifier.
There are two types of data types in java.

  1. Primitive Data Type
  2. Non-Primitive Data Type
1.Primitive Data Type
 A primitive data type can be of eight types :
  1. The char:It is a single 16-bit uni code character.The minimum value is 0.It is used to store any character.Example:char letterC='C'.
  2. The byte:It is 8 bit integer data type. Value range from -128 to 127. Default value zero.example:byte b=11; 
  3. The short:It is 16 bit integer data type. Value range from -32768 to 32767. Default value zero. example:short s=15;
  4. The int: It is 32 bit integer data type. Value range from -2147483648 to 2147483647. Default value zero. example:int i=10;
  5. The long:It is 64 bit integer data type. Value range from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Default value zero. example:long l=10001;
  6. The float:It is 32 bit float data type. Default value 0.0f. example:float ff=10.5f;
  7. The double:It is 64 bit float data type. Default value 0.0d. example:double db=10.266
  8. The Boolean:This data type represent one bit of information.there are only two possible values:True and False. example: boolean one=True;
2.Non-Primitive Data Type
A reference data type is used to refer to an object. A reference variable is declare to be of specific and that type can never be change.



Friday, 8 January 2016

BASIC PROGRAM OF JAVA

Writing the "HELLO WORLD" program.

class sample {
public static void main(String args [ ]) {
System.out.println("hello world");
}
}


OUTPUT :