Learn Java

Learn Java This Page is for those who want to learn java .
In a very comforting and friendly enviornment

First of all you have to install a java package to your computer system.

Here are the steps to set path



  • Once the path is set you can go to command prompt and run the command java.
  • If it runs, it will execute and display several statements.
  • If the path is not properly set the java command will show error. That clearly means that you have made any mistake and either reinstall your java or set the path again or even do both again.
  • Now for compiling your first program you must write this source code below in notepad of your computer and store(Save) a program in any folder(Default : My documents ) and remember save the program similar to the name of class followed by extention(.java)




  • After saving go to command prompt and reach in the folder where the program is stored.
  • Once you have reached the folder now you are ready to compile your set of code.
  • to compile use command javac then file name and . and then java and press enter.
              For example- javac hello.java(press enter)
  • if the cursor moves to next line there is no error, other wise there is any error and compiler will point it out.
  • If every thing is normal without any error you can now run your program by java command
               For example-java hello(Press Enter)

  • If the message is displayed properly
                      Congratulation!Good Going.
  • Now you have to work on integers(Whole Numbers).
  • But in java data flows in streams(strings) so we that strings are character data types .
  • To use integers java provides a function parseInt to convert string containing numerical values to integer and this method is saved in Integer calss
  • Syntax to use this function is as follows 
                                   int variable_name=Integer.parseInt(arguement);

  • Go on and check a code for getting numbers from user and print a sum.        




  • Here we are taking numbers from user from command line.
  • So compile program similarly.
  • Running program is a bit different as we have to provide 2 values
                               java add 2 5

  • we will get 7(Sum) as a result in the very next line if program is correct.
         Now as we can  get input from user by command line. we can move further and know the functioning of several operators

                       Operator

  • We have several types of operatorsn in Java

         1. Assignment operators 

           These are the various operators thich are used to assign values to the variables
              some examples of these operators are : =,+=,-=
           a program to view their implimentation.
                     
OperatorDescriptionExample
=Simple assignment operator, Assigns values from right side operands to left side operandC = A + B will 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 operandC += A is equivalent to C = C + A
-=Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operandC -= A is equivalent to C = C - A
*=Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operandC *= A is equivalent to C = C * A
/=Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operandC /= A is equivalent to C = C / A
%=Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operandC %= A is equivalent to C = C % A
 A dedicated program is as follows




   2. The Arithamatic Operators

  The arithamatic operators are used in mathematical expressions in the same way that they are used in algebra. The following table lists the arithamatic operators:

   Assume  integer values a holds 10 and b holds 20:

OperatorDescriptionExample
+Addition - Adds values on either side of the operatorA + B will give 30
-Subtraction - Subtracts right hand operand from left hand operandA - B will give -10
*Multiplication - Multiplies values on either side of the operatorA * B will give 200
/Division - Divides left hand operand by right hand operandB / A will give 2
%Modulus - Divides left hand operand by right hand operand and returns remainderB % A will give 0
++Increment - Increases the value of operand by 1B++ gives 21
--Decrement - Decreases the value of operand by 1B-- gives 19
The related programs are:
  1.  addition shown above
  2. Subtration
  3. Multiplication
  4. Division
  5. Rmainder
  6. Incriment
  7. Decrement


3. Relational operator


 There are several relational operators available in java

 Assume A=10 and B=20

 Example:-

OperatorDescriptionExample
==Checks if the values of two operands are equal or not, if yes then condition becomes true.(A == B) is not true.
!=Checks if the values of two operands are equal or not, if values are not equal then condition becomes true.(A != B) is true.
>Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.(A > B) is not true.
<Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.(A < B) is 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 >= B) is not true.
<=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 <= B) is true.

4. The Bitwise operator


Java defines several bitwise operator, which can be applied to integer , long , double etc.

Bitwise operator works on and performs bit by bit. If a=60 and b=13 .

a=00111100

b=00001101
_________________

a&b=00001100

a|b=00111101

a^b=00110001

~a=11000011

The following table lists the bitwise operators:

Assume A=60 and b=13.

Example

OperatorDescriptionExample
&Binary AND Operator copies a bit to the result if it exists in both operands.(A & B) will give 12 which is 0000 1100
|Binary OR Operator copies a bit if it exists in either operand.(A | B) will give 61 which is 0011 1101
^Binary XOR Operator copies the bit if it is set in one operand but not both.(A ^ B) will give 49 which is 0011 0001
~Binary Ones Complement Operator is unary and has the effect of 'flipping' bits.(~A ) will give -61 which is 1100 0011 in 2's complement form due to a signed binary number.
<<Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand.A << 2 will give 240 which is 1111 0000
>>Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand.A >> 2 will give 15 which is 1111
>>>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 >>>2 will give 15 which is 0000 1111

5. The logical operators


   There are 3 basic logical operators in Java
 
OperatorDescriptionExample
&&Called Logical AND operator. If both the operands are non-zero, then the condition becomes true.(A && B) is false.
||Called Logical OR Operator. If any of the two operands are non-zero, then the condition becomes true.(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 && B) is true.

6. Conditional Operator(?:):


  Conditional operator is also known as ternary operator. This operator consist of three operands and is used to evaluate Boolean expressions. The goal of the operator is to decide which value should be assigned to the variable.

A Dedicated Program



  7. Instanceof Operator:


 This operator is used only for object refrence variables. The operator checks wheather the object is of a     particular type(class type or interface type).

  If the object reffered by the variableon left side of the operator passes the is a check for the class/interface type on the right side, then the result will be true

The dedicated program:





No comments:

Post a Comment