Friday, 21 March 2014

UNARY OPERATORS

The operators which acts upon one operand is called Unary Operator. There are three types of Unary operators in java

  • Unary minus operator(-)
  • Increment Operator(++)
  • Decrement Operator(--)
Unary Minus Operator(-)
This operator is used to negate a given value. Negation means to convert negative value into positive and Positive value into negative.
for example
int a=8;
int b=-4;
System.out.println(-a);   will display -8
System.out.println(-b);   will display 4

Increment Operator(++)
This value increases the value of variable by 1.for example
int a=3;
++a will make it a=4
a++ will make it a=5
if we use increment operator in left side of variable it is known as pre increment operator.pre increment operator first increment operator first increments the value then displays it.
if we use increment operator in right side of variable it is called post increment operator. It first displays the value then increments it.
int a=2
System.out.println(a);
System.out.println(++a);
System.out.println(a);
Output:
2
3
3
int b=3
System.out.println(b);
System.out.println(b++);
System.out.println(b);
Output:
3
3
4
Decrement Operator(--)
This operator is used to decrement the value of variable by 1.for example
int s=2;
--s  will make it s=1
s-- will make it s=0
if we use decrement operator in left side of variable it is known as pre decrement operator.pre decrement operator  first decrements the value then displays it.
if we use decrement operator in right side of variable it is called post decrement operator. It first displays the value then decrements it.
int a=2
System.out.println(a);
System.out.println(--a);
System.out.println(a);
Output:
2
1
1
int b=3
System.out.println(b);
System.out.println(b--);
System.out.println(b);
Output:
3
3
2

Monday, 10 March 2014

Operators in Java

Operators are the symbol that performs some operations.Operator acts on variables. Those variables are called operands.
for example: c*d
Here c and d are operands where as * is an operator.
If an operator acts upon single operand then it is known as unary operator.If it acts upon two operand, they are called binary operator. 
Arithmetic Operator
The operator that are used to perform fundamental operations like addition, subtraction, multiplication, division etc. are known as Arithmetic operator. There are five arithmetic operators in java. There are shown in the table.

Operator
Function
Example
Outcome
+
Addition Operator
44+50
94
-
Subtraction Operator
50-44
6
*
Multiplication Operator
44*50
2200
/
Division Operator
50/25
2
%
Modulus Operator
45%25
20

Note :-
  •  Addition operator in java is also used to join two  Strings.
  •   In java Strings are a type of class whereas in C/C++ It is a data type.

Logical Operators
These operators are used for logical operations or to construct compound condition, Compound condition is a combination of several simple sentences. There are Three logical operators-
  • && And Operator
  • || Or operator
  • ! Not Operator
For example- 
if(a==1 || c==1)
System.out.println("True");
In this statement any of a or c is equal to 1 then True will be shown

if(a==1 && c==1)
System.out.println("Fine");
In this statementif both a and c is equal to 1 only then "Fine " will be displayed


In next blog we will discuss on Unary  and Boolean Operators

Sunday, 9 March 2014

Literals

Literals represent a value that is stored in variables directly in the program.For example 
int x=234;
here values in the right side are called literals because these values are stored in the variables in the left side.As we have discussed earlier that there are different data types.So there are following types of Literals-

  • Integer Literals
  • Float Literals
  • Character Literals
  • String Literals
  • Boolean Literals 
Integer Literals
It represents fixed integer values like 100,22,33.For example
int x=34;
Float Literals
It represents fractional numbers like 22.456, 32.006.For example
float c=44.66f;
Character Literals
It represents character values like a,b,B,'D'.for example
char ch='A';
String Literals
It represents String values like "hello","Anil".for example
String s1="Hello";
Boolean Literals 
It represents only two values either true or false



In next blog we will discuss about Operators in Java

 

Saturday, 8 March 2014

Data Types(Character & Boolean)

Today we will discuss Character data types and Boolean data types.
Character Data Types
This data type represents single characters like a,p ,m,s,r.To store char data type JVM takes 2 byte memory.It has range from 0 to 65535.There are no negative chars.The standard set of characters known as ASCII still ranges from 0 to 127 as always, and the extended 8-bit character set, ISO-Latin-1, ranges from 0 to 255. Since Java is designed to allow programs to be written for worldwide use, it makes sense that it would use Unicode to represent characters. Of course, the use of Unicode is somewhat inefficient for languages such as English, German, Spanish, or French, whose characters can easily be contained within 8 bits. But such is the price that must be paid for global portability.Unicode defines a fully international character set that can represent all of the characters found in all human languages. It is a unification of dozens of character sets, such as Latin, Greek, Arabic, Cyrillic, Hebrew, Katakana, Hangul, and many more. For this purpose, it requires 16 bits. Thus, in Java char is a 16-bit (2 byte)type.
To declare character data types we use a keyword "char".

It can be explained through a Java program
class Data
{

public static void main(String args[]) 
{
char ch, cH1;
ch = 83; // code for S
cH1 = 'A';
System.out.println("ch : "+ch);
System.out.println("cH1 : " + cH1);
}
}

Output:
ch : S
cH1: A

Boolean Data Types
Boolean data types represent  any of the two values true or false. JVM uses 1 bit memory to store boolean value . We use "boolean " keyword to declare boolean data types.
 It can be explained through a Java program

class Data
 {
    public static void main(String args[]) 
  {
    boolean b;
    b = false;
    System.out.println("b is " + b);
     b = true;
     System.out.println("b is " + b);
   }
}

output:-
 false
true

You all readers are requested to post your queries regarding these topics

Thursday, 6 March 2014

Data Types in Java

In Java there are eight primitive Data Types.they are: byte, short, int, long, char, float, double, and boolean.
We arrange these data types in four groups: Integer,Float,Character,Boolean.

  • Integers This group includes byte, short, int, and long, which are for whole-valued signed numbers.
  •  Floating-point numbers This group includes float and double, which represent numbers with fractional precision.
  •  Characters This group includes char, which represents symbols in a character set, like letters and numbers.
  • Boolean This group includes boolean, which is a special type for representing true/false values
Integer Data Types
these data types represent integer numbers,i.e.,the numbers which do not have any fractional part.for example-34,456.these are again sub divided into byte,short,int and long types.
byte
The smallest integer type is byte. It is a signed 8-bit type that has a range from –128 to 127.
Variables of type byte are especially useful when you’re working with a stream of data from
a network or file. They are also useful when you’re working with raw binary data that may
not be directly compatible with Java’s other built-in types.

short
short is a signed 16-bit type. It has a range from –32,768 to 32,767. It is probably the least-used Java type.
int
The most commonly used integer type is int. It is a signed 32-bit type that has a range from –2,147,483,648 to 2,147,483,647. In addition to other uses, variables of type int are
commonly employed to control loops and to index arrays. When byte and short values are used in an expression they are promoted to int when the expression is evaluated.
long
long is a signed 64-bit type and is useful for those occasions where an int type is not large enough to hold the desired value. The range of a long is quite large. Their range is from -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807. This makes it useful when big, whole numbers are needed.


How to declare variables of Integer Data types
To declare Byte we use keyword "byte" as follows-
byte x;
x=10;
here x is a variable of Byte type integer whose value is 10
To declare Short data type the keyword "short" is used.
short=y;
To declare Int data type keyword "int " is used
int a;
To declare Long data type we use keyword "long"
long l=15L;
here we should write"L" at the end of statement. If we don't write it ,the JVM will allot only 2 byte to it.If we attach "L " the JVM will consider it as long value and will allot 8 byte.

In next blog we will discuss Character and Float data types.
So keeping reading this blog daily.And follow it.

Wednesday, 5 March 2014

A simple Java program

In this blog we will start  java progamming.
TO Print A simple message in java.
before writing a program we should keep these things in mind-
  1. A java program have atleast one class.
  2. It should have main method.
  3. It should be saved as classname.java
  4. To compile it we write "javac classname.java"
  5. To run it we write"java classname"
class first
{
public static void main(String args[])
{
System.out.println("My First Java Program");
}
}
Output:-My First Java Program

In main method we have passed arguments as String args[] because without writing it the code public static void main() will be compiled but JVM will not run it .It is necessary to pass it as argument.To print any message we have to call print() method of PrintStream class.To call it we write System.out.print();



In next blog we will discuss about variables and Data Types.

Sunday, 2 March 2014

Difference Between C++ and Java

In this blog we will discuss the differences between java and C++

  • C++ is not pure object oriented programming language,because it is possible to write a program without using class. Whereas Java is pure object oriented programming language,because in java we cannot write any program without a class.It needs atleast one class.
  • Pointers are available in C++ whereas in Java we can use or create any pointer.
  • In C++, User has to allocate or deallocate  memory locations itself  Whereas in java allocation and deallocation of  memory is the responsibility of JVM.
  • C++ has goto statement.But Java does not have goto statement.
  • Multiple Inheritance is available in C++.In Java no multiple inheritance is possible.
  • In C++ Operator overloading is possible. But in Java Operator overloading is not possible.
  • #define, typedef and header files are available in C++. Whereas in Java #define,typedef and header files are not available but there are means to achieve them.
  • There are mainly three access specifier in C++:- private,public,protected. Whereas in java there are four access specifier :private,public,default and protected.
  • In C++ Constructors and destructors are available. Whereas in Java only Constructors are available.
Parts of java
Sun microsystems Inc. has divided java in three parts-

  1. Java SE-It is the Standard Edition which contains basic core Java classes.This is used to develop standard applets and application.
  2. JavaEE-It is the Java Enterprise Edition which contains classes that are beyond Java Se.It concentrates on providing business solutions on network.
  3. Java ME- It is the Java Micro Edition.It is for developers who developes code for portable devices.Code on these devices are vewry small and take less memory.