Java Variables in addition to Operators


Java Modifiers, Variables in addition to Operators
 

i) Modifiers inwards Java

ii) Java Variables

iii) Java operators
-----------------------------
i) Modifiers inwards Java
Modifiers are keywords that nosotros add together to those definitions to alter their meaning.

a) Access Modifiers

b) Non-Access Modifiers
--------------------------
a) Access Modifiers
We role access modifiers to define access command for classes, methods in addition to variables.

Four Access Modifiers

i) private
The mortal access modifier is accessible alone within class.

Ex:

private int a =100;

2) default
If nosotros don't specify whatever modifier hence it is treated equally default, this tin live accessible alone within package.

class Sample{
.
.
}

3) protected
The protected access modifier is accessible within package, exterior of the bundle but through Inheritance only.

protected course of teaching Sample{
.
.
}

4) public
public access modifier is accessible everywhere.

public course of teaching Sample {
.
.
}
---------------------------------------------------
Modifier
Within Class
Within Package
Outside of the Package
(By Sub Class)
Outside of the Package
private
Y
N
N
N
default
Y
Y
N
N
protected
Y
Y
Y
N
public
Y
Y
Y
Y
 

b) Non Access Modifiers
1) static
static modifier is used practice classes, methods in addition to variables.

Ex:

static int a =10;

static void int add(){
.
.
}

2) concluding
final modifier for finalizing of classes, methods in addition to variables.

Ex:
final int a =100;
.
.
.
a=200; //Error
---------------
int a =100;
.
.
.
.
a =200;
------------------
3) abstract
abstract modifier is to practice abstract classes, abstract methods

ex:

abstract course of teaching Sample{
.
.
}
-------------------------------
ii) Java Variables
1) What is Variable?
A named retentiveness place to shop the temporary information within a program.

Two types of memories inwards Computer environment

a) Primary retentiveness (RAM)

b) Secondary retentiveness (HDD, DVD, USB movement etc...)
----------------------------
2) Declaration of Variables
Java supports Explicit annunciation of Variables.

Syntax in addition to Examples:

dataType variableName;

int a;
-------------
dataType variablename=value;

int b=20;
---------------
dataType variable1, Variable2, variable3;

int a, b, c;
-----------------
dataType variable1=value; variable2=value; varible3=value;

int a=10; b=20; c=30;
------------------------------------
3) Assign values to variables
a) Initialization

b) Reading

Ex:

int a=100; //Initialization

int a=10;
int b;
b=a; //Reading
---------------------------------
4) Variable Naming Restrictions
> Java variables are illustration sensitive,

> Java variable nurture should start amongst a missive of the alphabet or $ or _

Ex:

myvar(Correct)
MYVAR
$myvar
_myvar
myvar_1
--------------
1myvar(Incorrect)
*myvar
----------------
> Variable names should non check amongst Java keywords/Reserved words.

> Must live unique inwards the range of declaration.

> Variable names Must non overstep 255 characters.
-------------------------------------------

5) Types of Variables
Three types of variables inwards Java
a) Local variable(Local variable is declared inwards methods or blocks.)

b) Instance variable(Instance variables are declared inwards a course of teaching but exterior of a method or whatever block)

c) Class/Static variableA Variable that is declared equally static, It cannot live local.
----------------------------------
Example:

package xyza;

public course of teaching VariablesExample {
//a Variable is a Class/Static variable
static int a =100;

//mysalary variable is a Local variable.
public int salary(){
    int mysalary =10000+2000+1500;
    mysalary=mysalary + a;
    furnish mysalary;
}

public static void original (String[]args){
//Instance variable
int b =200;
System.out.println(a);//100
System.out.println(b); //200   
       
VariablesExample obj= novel VariablesExample();   
System.out.println(obj.salary());
// i is a Local Variable   
for (int i=1; i<=5; i++){
    System.out.println(i);
    System.out.println(a);
    System.out.println(b);
}
}
}
----------------------------------
iii) Java Operators
Important Categories of Operators

a) Arithmetic Operators

b) Relational Operators

c) Assignment Operators

d) Logical Operators
-------------------------------
a) Arithmetic Operators
1) Addition + (for Addition, String concatenation)

2) Subtraction - (for Subtraction, Negation)

3) Multiplication *

4) Division /

5) Modules %

6) Increment ++

7) Decrement --
---------------------------
Example:

public course of teaching OperatorsExample {
    world static void original (String [] args){
        int a =10, b=5;
        String c ="Selenium", d= "Testing";

System.out.println("Addition of a, b is: "+ (a+b));//Addition of a, b is: 15
System.out.println("Subtraction of a, b is: "+ (a-b));       
System.out.println("Multiplication of a, b is: "+ (a*b));   
System.out.println("Division of a, b is: "+ (a/b));   
System.out.println("Modules of a, b is: "+ (a%b));

b=10;
a = ++b;
System.out.println(a);//11

b=10;
a = --b;
System.out.println(a);//9
}
}
---------------------------------------
b) Relational Operators
1) ==

2) !=

3) >

4) >=

5) <

6) <=
----------------------------------
Note: Relational Operators furnish Boolean / Logical result

Example:

public course of teaching OperatorsExample {
    world static void original (String [] args){
        int a =10, b=20;
        System.out.println((a>b));//false
        System.out.println((a>=b));//false
        System.out.println((a==b));//false
       
        System.out.println((a<b));//true
        System.out.println((a<=b));//true
        System.out.println((a!=b));//true
}
}
------------------------------------------------
d) Logical Operators
1) Logical Not Operator  !

2) Logical And Operator &&

3) Logical Or Operators ||

Result Criteria

Not operator
--------------
Operand1    Operand2    Result
--------------------------------------
true               true         false
true               false        true
false              true         true
false              false        true
--------------------------------------
And operator
--------------
Operand1    Operand2    Result
--------------------------------------
true             true            true
true             false           false
false            true            false
false            false           false
--------------------------------------
Or Operator

Operand1    Operand2    Result
--------------------------------------
true             true             true
true             false            true
false            true             true
false            false            false
--------------------------------------
Example:

public course of teaching OperatorsExample {
    world static void original (String [] args){
        boolean a =true, b=false;
        System.out.println(!(a && b));//true
        System.out.println((a && b));//false
        System.out.println((a || b));//true
    }       
}
--------------------------------------
public course of teaching OperatorsExample {
    world static void original (String [] args){
        int a =1000, b=500, c=7000;

if ((a>b) && (a>c)){
    System.out.println("A is a Big Number");
}
else{
    System.out.println("A is Not a Big Number");   
}
}       
}
--------------------------------------
c) Assignment Operators
1) Assignment Operator

=

a=10;

2) Add in addition to Assign +=

3) Subtract in addition to assign

4) Multiple in addition to assign
-------------------------------
Example:
public course of teaching OperatorsExample {
    world static void original (String [] args){
        int a =10;

System.out.println(a);//10
a+=10;
System.out.println(a);//20

a-=10;
System.out.println(a);//10

a*=10;
System.out.println(a);//100
}       
}
-----------------------------------------------------------
Bitwise Operators
> Java defines several bitwise operators, which tin live applied to the integer types, Bitwise operator works
   on bits in addition to performs bit-by-bit operation.

i) The bitwise & operator performs a bitwise AND operation.

ii) The bitwise ^ operator performs a bitwise exclusive OR operation.

iii) The bitwise | operator performs a bitwise XOR operation.

Sumber http://www.gcreddy.com/
Post a Comment (0)
Previous Post Next Post