Fundamentals of Java

Posted: June 9, 2012 by bhumika289 in JAVA

Identifiers

Identifiers are names used for classes, variables and methods. Points to remember:

  • All identifiers should begin with a letter (A to or a to z ), currency character ($) or an underscore (-).  eg. $name, _name, __1_name ,name
  • After the first character identifiers can have any combination of characters.
  • A keyword cannot be used as an identifier.
  • Identifiers are case-sensitive.

Keywords

The following list shows the reserved/keywords in Java.

abstract assert boolean break
byte case catch char
class const continue default
do double else enum
extends final finally float
for goto if implements
import instanceof int interface
long native new package
private protected public return
short static strictfp super
switch synchronized this throw
throws transient try void
volatile while

Variables

  • Basic unit of storage in a java program.
  • Every variable has scope and life time throughout the program.
  • A block is begins with an opening curly brace and ends with a closing curly brace.A block defines a scope.
  • Most other computer languages define two categories of scopes: global and local.

Declaring variables:
syntax:       type identifier1=val , identifier2; where,

  • type: datatype or classname.
  • identifier: variable name.
  • optional initializer to directly initialize the variable with a value.
example:
    int a = 29, b;

 Dynamic Initialization:Variables can be initialized dynamically as shown below:

    int a = 29, b;
b= a*a;

Symbolic Constants

  • Value is fixed within the scope.
  • Declared using the ‘final’ keyword.

example:

    final double PI = 3.414;

Java Literals

  • Representation of a fixed value.
  • Can be assigned to any primitive type variable.

example:

    int a = 29;
String s = “Welcome to Engineering Adda”;

Escape sequences for String and char literals:

Notation

Character represented

\n Newline
\r Carriage return
\f Formfeed
\b Backspace
\s Space
\t Tab
\” Double quote
\’ Single quote
\\ backslash
\ddd Octal character
\uxxxx Hexadecimal UNICODE character

Data Types in Java

There are two data types available in Java:

  1. Primitive Data Types
  2. User-defined Data Types

1. Primitive Data Types:

Primitive Data Types

2. User-defined Data Types:

  • Reference variables are created using defined constructors of the classes.
  • They are used to access objects.
  • These variables are declared to be of a specific type that cannot be changed.
  • Class objects, and various type of array variables come under reference data type.
  • Default value of any reference variable is null.
  • A reference variable can be used to refer to any object of the declared type or any compatible type.

example:

     Fruit fruit = new Fruit(“apple”);

Operators

Symbol that tells the computer to perform mathematical and logical calculation.   Arithmetic Operators:

Operator Description Example
+ Addition – Adds values on either side of the operator A=10, B=12, A + B=22
Subtraction – Subtracts right hand operand from left hand operand A=10, B=12, A – B=-2
* Multiplication – Multiplies values on either side of the operator A=10, B=12, A * B=120
/ Division – Divides left hand operand by right hand operand A=10, B=10, B / A will give 1
% Modulus – Divides left hand operand by right hand operand and returns remainder A=10, B=9, B % A will give 1
++ Increment – Increase the value of operand by 1 B=20, B++ gives 21
Decrement – Decrease the value of operand by 1 B=20, B– gives 19

Relational Operators:

Operator Description Example
== Checks if the value of two operands are equal or not, if yes then condition becomes true. A=19, B=20, (A == B)=false.
!= Checks if the value of two operands are equal or not, if values are not equal then condition becomes true. A=19, B=20, (A != B)=true.
> Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. A=19, B=20, (A > B)=false.
< Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. A=19, B=20, (A < B)=true.
>= Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. A=19, B=20, (A >= B)=false.
<= Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. A=19, B=20, (A <= B)=true.

Bitwise Operators:

Operator Description Example
& Binary AND Operator copies a bit to the result if it exists in both operands. A=0011, B=1110, (A & B)=0010i.e. A=3, B=15, (A & B)=2
| Binary OR Operator copies a bit if it exists in eather operand. A=0011, B=0110, (A | B)=0111i.e. A=3, B=6, (A | B)=7
^ Binary XOR Operator copies the bit if it is set in one operand but not both. A=1010, B=0011, (A ^ B)=1001i.e. A=10, B=3, (A ^ B)=9
~ Binary Ones Complement Operator is unary and has the efect of ‘flipping’ bits. A=0011, (~A)=1100i.e. A=3, (~A)=12
<< Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. A=0001, A << 2 =0100i.e. A=1, A << 2 =4
>> Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. A=0100, A >> 2 =0001i.e. A=4, A >> 2 =1
>>> Shift right zero fill operator. The left operands value is moved right by the number of bits specified by the right operand and shifted values are filled up with zeros. A=1101, A >>>2 =0010i.e. A=13, A >>>2 =2

Logical Operators:

Operator Description Example
&& Called Logical AND operator. If both the operands are non zero then then condition becomes true. A=11, B=01, (A && B) is false.
|| Called Logical OR Operator. If any of the two operands are non zero then then condition becomes true. A=11, B=01, (A || B) is true.
! Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. A=11, B=01, !(A && B) is true.

Assignment Operators:

Operator Description Example
= Simple assignment operator, Assigns values from right side operands to left side operand C = A + B assign value of A + B into C
+= Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand C += A equivalent to C = C + A
-= Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand C -= A equivalent to C = C – A
*= Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand C *= A equivalent to C = C * A
/= Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand C /= A equivalent to C = C / A
%= Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand C %= A equivalent to C = C % A
<<= Left shift AND assignment operator C <<= 2 same as C = C << 2
>>= Right shift AND assignment operator C >>= 2 same as C = C >> 2
&= Bitwise AND assignment operator C &= 2 same as C = C & 2
^= bitwise exclusive OR and assignment operator C ^= 2 same as C = C ^ 2
|= bitwise inclusive OR and assignment operator C |= 2 same as C = C | 2

Conditional Operator ( ? : ):

  • Also known as the ternary operator.
  • Consists of three operands.
  • Used to evaluate boolean expressions.
  • The goal of the operator is to decide which value should be assigned to the variable.

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

  • Used only for object reference variables.
  • Checks whether the object is of a particular type(class type or interface type).

syntax:   ( Object reference variable ) instanceOf  (class/interface type)
Precedence of Java Operators:

Category  Operator  Associativity 
Postfix () [] . (dot operator) Left to right
Unary ++ – – ! ~ Right to left
Multiplicative * / % Left to right
Additive + – Left to right
Shift >> >>> << Left to right
Relational > >= < <= Left to right
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %= >>= <<= &= ^= |= Right to left
Comma , Left to right
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s