Comments In Java
Whenever we write a program, we should first think about writing comments.
Comments are description about the features of a program.
There are three types of comments in java
- Single line comments
- Multi line comments
- Java documentation comments
Single Line Comments
- The single line comment is used to comment only single line.
- These comments start with double slash symbol ' // '.
- After this symbol // , whatever is written till the end of the line is taken as a comment.
Eg:
//This is a one line comment.
Multi Line Comments
- These comments are used for representing multiple lines.
- Multi line comments start with /* and ends with */.
- In between /* and */ , whatever is written is treated as a comment.
Eg.
/* This is a multi line comment.
This is line two of the comment.
This is line three of the comment.*/
Java Documentation Comments
- These comments start with /** and end with */.
- These comments are used to provide description for every features in a java program.
Eg.
/** description about a class*/
class code
/*description about a method*/
method code
Data Types in Java
A variable represents a memory location which holds ' data'.
- Integer Data Types
- Float Data Types
- String Data Types
- Boolean Data Types
Integer Data Types
These data types represent integer numbers.
Eg.
int x ;
x=25;
Here, x is a variable, which can store int(integer) type of data. x is a variable and = represents that the value 25 is stored in to x.This value stored in to x is also called literal.
Float Data Types
These data types are useful to represent numbers with decimal point.
Eg.
float x = 3.426 ;
Character Data Types
These data type represents a single character like a,b,d,etc,
Eg.
char ch = 'x' ;
Here, we are storing the single character 'x' in to the variable ch. x is also called character literal. Whenever the character literals are written they should be enclosed inside the single quotes.
String Data Types
A string represents a group of characters.
Eg.
String str = " anu " ;
Now the string type variable str contains "anu". Note that any string written directly in a program should be enclosed by using double quotes.
Boolean Data Types
Boolean data type represent any of the two values - true or false.
Eg.
boolean response = true;
Literals
A literal represents a value that stored in to a variable directly in the program.
As the data type of the variable changes, the type of literal also changes. so, we have different types of literals.
- Integer literals
- Float literals
- Character literals
- Boolean literals
- String literals
Eg.
char gender = 'M' ;
boolean result = false ;
The right hand side values are called literals. Because these values are being stored in to the variable shown at the left hand side.
Operators In Java
An operator is a symbol that performs operation. An operator acts on variables called operands.
Eg;
a + b => a,b are called operands
+ is operator
Arithmetic Operator
These operators are used to perform fundamental arithmetic operations like addition, Subtraction,etc.
+ Arithmetic Operator
- Subtraction Operator
* Multiplication Operator
/ Division Operator
% Modulus operator
Unary Operator
As the name indicates, unary operator act on only one operand.
There are three kinds of unary operators :
- Unary Minus Operator (-)
- This operator is used to negate a given value.
- Negation means converting a negative value in to positive and vise versa.
int x = 3;
System.out.println(-x); will display -3.
System.out.println(-(x)); will display 3.
2. Increment Operator (++)
- This operator increases the value of variable by 1.
- Writing ++ before a variable is called pre incrementation.
- Writing ++ after a variable is called post incrementation.
Eg;
Finding the difference between pre and post increment of x
Pre incrementation
int x =1;
System.out.println(x);
System.out.println(++x);
System.out.println(x);
Output:
1
2
2
Post incrementation
int x = 1;
System.out.println(x);
System.out.println(x++);
System.out.println(x);
output:
1
1
2
3. Decrement operator (--)
- This operator is used to decrement the values of a variable by 1.
- Writing -- before a variable is called pre decrementation.
- Writing -- after a variable is called post decrementation.
Eg.
int x = 1;
--x will make the value of x as 0
x-- will make the value of x as -1
Assignment Operator(=)
- This operator is used to store some values in to a variable.
- It is used in 3 ways:
- It is used to store a value in to a variable.
Eg. int x = 7 ;
2. It is used to store value of a variable in to another variable.
Eg. int x = y ;
3. It is used to store value of an expression in to a variable.
Eg. int x = y+z-2 ;
Relational Operator
These operators are used for the purpose of comparing. Relational operators are of 6 types.
- > greater than
- < less than
- >= greater than or equal to
- <= less than or equal to
- == equal to operator
- != not equal to operator
Logical Operators
Logical operators are used to construct compound conditions. A compound condition is a combination of several simple conditions. Logical operators are of three types.
- || or operator
- && and operator
- ! not operator
Eg.
if(a == 1 || b = = 1 || c ==1) System.out.println("yes");
Here, there are three conditions a==1 b==1 and c==1 which are combined by || (or operator). In this case, if either of the a or b or c value becomes equal to 1 , "yes "will be displayed.
Click on the link below to know about

No comments:
Post a Comment