< ;
Breaking News
Loading...

Sunday, August 04, 2019

java gui programming revision tour-1 solutions

JAVA GUI PROGRAMMING REVISION TOUR 1 SOLUTIONS CLASS XII

Image result for java sumita arora

What is Java?

Java is a popular programming language, created in 1995.
It is owned by Oracle, and more than 3 billion devices run Java.
It is used for:
  • Mobile applications (specially Android apps)
  • Desktop applications
  • Web applications
  • Web servers and application servers
  • Games
  • Database connection
  • And much, much more!
1.What is RAD?
Ans: Rapid Application Development (RAD) describes a method of developing software through the use of pre-programming tools or wizards. The pre-programmed tools or controls are simply dropped on a screen to visually design the interface of application.
2 What are RAD tools?
Ans: RAD tools are the tool that enables one to create applications in shorter time as compared to conventional language.
3 What is event? What is message? How is it related to an event?
Ans:  An event refers to the occurrence of an activity.
 A message is the information/request sent to the application.
 Each time an event occurs, it causes a message to be sent to the operating system.

4 What are properties? What is property window?
Ans:  Properties are the attributes of graphical controls.
 Properties window displays the editable settings for the currently selected component.
5 What are containers? Give examples.
Ans: A container is a control that can hold other controls within it. For example, JPanel, JFrame, JApplet and JDialog are
container classes.
6 What are child controls?
Ans: The container control is the one that can contain/hold other controls in it. The contained controls are known as child
controls.
7 What happens to container control and its child controls if you delete
(i) The container control,
(ii) A child control
(iii) All child controls?

Ans: (i) If container control is deleted then with container control child control will be also deleted
(ii) If a child control is deleted then the container control will be remain and a child control will be deleted.
(iii) If all child controls is deleted then the container control will be remain and all the child control will be
deleted.
8 Name the character set supported by java.
Ans: Java uses the Unicode character set.
9 What are keywords? Can keywords be used as identifiers?
Ans:  Keywords are the words that convey a special meaning to the language compiler. These are reserved for special purpose.
 Keywords cannot be used as identifiers.

10 What is an identifier? What is the identifier forming rule of java?
Ans:  Identifiers are user define names for different parts of program.
 Identifier forming rules of java state the following:
1. Identifiers can have alphabets, digits, and underscore and dollar sign characters.
2. They must not be a keyword or Boolean literal or null literal.
3. They must not begin with a digit.
4. They can be of any length.
5. Java is case sensitive i.e., upper-case letters and lower case letters are treated differently.

11 Is java case sensitive? What is meant by the term ‘case sensitive’?
Ans:  Java is case sensitive.
 Case sensitive meant upper-case letters and lower case letters are treated differently.
12 Which of the following are valid identifiers and why/why not?
Data_rec, _data, 1 data, data 1, my.file, asm, switch, goto, break
Ans: 1. Data_rec: Valid identifiers
2. _data: Valid identifiers
3. 1 data: Not valid because they must not begin with a digit
4. data 1: not valid because space is not allowed.
5. my.file: not valid because special character not allowed.
6. Asm: Valid identifiers
7. Switch: not valid because reserved keyword is not allowed.
8. goto: not valid because reserved keyword is not allowed.
9. break: not valid because reserved keyword is not allowed.
13 What are literals? How many types of integer literals are available in java?
Ans:  Literals are data items that never change their value during a program run.
 integer-literal (decimal, octal, hexadecimal)

14 How many types of integer constant are allowed in java: How are they written?
Ans: There are three types of integer constant are allowed in java.
1. Decimal: 17
2. Octal: 011
3. Hexadecimal: 0XBC1

15 What kind of constant are the following: 14, 011, 0X2A, 17, 014, 0XBC1?
Ans: 1. Decimal: 14, 17
2. Octal: 011, 014
3. Hexadecimal: 0XBC1, 0X2A

16 Write the following real constants into exponent form: 23.197, 7.214, 0.00005, and 0.319.
Ans: 1. 23.197 -> 2.3197 × 102 =2.3197E02
2. 7.214 -> 0.7214 x 101 = 0.7214E01
3. 0.00005 -> 0.000005 x 101 =0.000005E01
4. 0.319 -> 0.0319 x 101 = 0.0319E01
17 What will be the result of a=5/3 if a is
(i) float (ii) int?
Ans: (i) float-> 1.0
(ii) int->1

18 The expression 8%3 evaluates to _____________.
Ans: 2
19 What will be the value of j=--k+2*k+l++ if k is 20 initially?
Ans: 57
20 What will be the value of p=p*++j where j is 22 and p=3 initially?
Ans: 69
21 Write equivalent java expression for the following expressions:
(i) ut +
11
22 ft2
(ii) |a|+b>=|b|+a
(iii) �
3333+5555
5555+3333 − 888888
222222�
(iv) ee�22xx22−4444�
Ans: (i) x=u*t+(1/2)*f*t*t;
(ii) a+b>=b+a;
(iii) x=((3*x+5*y)/(5*x+3*y)-(8*x*y)/(2*y*x))
(iv) Math.pow(e,(2*x*x-4*x))

http://cbsecsnip.in

Page 2 of 13

22 What is meant by implicit and explicit type conversion?
Ans:  An implicit type conversion is a conversion performed by the compiler without programmer’s intervention.

 An explicit type conversion is user-defined that forces an expression to be of specific type.

23 What do you mean by type casting? What is type cast operator?
Ans:  The explicit conversion of an operand to a specific type is called Type Casting.
 Type cast operator is used for Casts or converts a value to the specified type.

24 What will be the resultant type of the following expressions if bh represents a byte variable, i is an int variable, fl is a
float variable and db is a double variable?
(i) Bh-i+db/fl-i*fl+db/i.
(ii) (int) (fl+db)
Ans: (i) Double
(ii) Double
25 What is the significance of null statement?
Ans: A null statement is useful in those instances where the syntax of the language requires the presence of a statement
but where the logic of the program does not.
26 What are three constructs that govern statement flow?
Ans: Sequence
Selection
Iteration
27 One out of several different alternatives can be selected with the help of which statement?
Ans: Switch statement
28 What is significance of a break statement in a switch statement?
Ans: The break statements are necessary because without them, statements in switch blocks fall through: All statements after the matching case label are executed in sequence, regardless of the expression of subsequent case labels, until a
break statement is encountered.
29 Write one limitation and one advantage of switch statement.
Ans: Advantage:
 The switch statement is a control statement that handles multiple selections and enumerations by passing
control to one of the case statements within its body.
Limitation:
 Logical operators cannot be used with switch statement.
case k>=20;
 Switch case variables can have only int and char data type.
30 What is the effect of absence of break in a switch statement?
Ans: without the break statement, statements in switch blocks fall through
31 What is the significance of default clause in a switch statement?
Ans: Default clause is used for the else situation in a switch statement
32 What are iteration statements? Name the iteration statements provide by java.
Ans: The iteration statements allow a set of instructions to be performed repeatedly until a certain condition is fulfilled.
Java provides three kind of looping statements: for loop, while loop, do-while loop.
33 Which elements are needed to control a loop?
Ans: 1. Initialization Expression(s).
2. Test Expression.
3. Update Expression(s).
4. The Body-of-the-Loop.

34 What is meant by an entry-controlled loop? Which java loops are entry-controlled?
Ans:  Entry-controlled loops: Here loop condition is tested before entering in to the loop.
 for and while loops are entry-controlled loop

35 What is meant by an exit-controlled loop? Which java loops are exit-controlled?
Ans:  Exit-controlled loops: Here loop body is executed first and then continuation condition is evaluated.

 Do-while loop is exit-controlled loop.

36 Write for loop that displays the numbers from 51 to 60.
Ans: for(int i=51;i<60;i++)
37 Write for loop that displays the numbers from 10 to 1 i.e., 10, 9, 8........ 3, 2, 1
Ans: for(i=10;i>=1;i--)

Type B: Short/Long Answer Questions

1 How are event, message, methods and properties interrelated?
Ans: Event, message, methods and properties all are used for accomplishing the Event handling system for GUI application.
All 4 of them are interrelated with each other by this way –
Event is occurrence of something happened, when event occurred message is sent to application based on which
method takes an action which and depending on the method’s action properties are sets or gets.
2 Name at least three common controls. Also give some of their properties.
Ans 1. Label

Properties: background, border, font, foreground, icon, text
2. TextField
Properties: background, border, font, foreground, editable, focusable, text
3. Password Field
Properties: background, font, foreground, text, echoChar

3 How are keywords different from identifiers?
Ans:
Keywords Identifiers
 Keywords are reserved words used for
special purpose and must not be used as
normal identifier names.
 Example : do, if, catch, final

 Identifiers are fumdamental building blocks of a program
and are used as the general terminology for the names
given to different parts of the program.
 Example: Myfile, _ds, date_7_9

4 What are literals in java? How many types of literals are allowed in java?
Ans:  Literals are also referred to as constants. They never change their value during program run.

 There are several kinds of literals available in Java. These are:
integer-literals, character-literals, floating literals, boolean literals, string literals, the null literals.

5 Determine the data type of the expression
(i) �
111111(11−pppp)
(qq+rr) � − � (pp+rr)/ss
(llllllll)(ss+pp)

(ii) �
2222+3333
5555+6666 + 8888
5555�4

If p, x is an int, r, w is a float, q, y is a long and s, z is double, t is short and u is long double.
Ans: (i) double
(ii) double
6 Given the following code fragment:
if (a == 0)
System.out.println (“Zero”);
if (a == 1)
System.out.println (“One”);
if (a == 2)
System.out.println (“Two”);
if (a == 3)
System.out.println (“Three”);
Write an alternative code (using if) that saves on number of comparisons.
Ans: if(a==0)
{
System.out.println(“Zero”);
}

http://cbsecsnip.in

Page 4 of 13

else if(a==1)
{
System.out.println(“One”);
}
else if(a==2)
{
System.out.println(“Two”);
}
else if (a == 3)
{
System.out.println(“Three”);
}
7 Rewrite the following fragment using switch:
if (ch == ‘E’)
eastern++;
if (ch == ‘W’)
western++;
if (ch == ‘N’)
northern++;
if (ch == ‘S’)
southern++;
else
unknown++;
Ans: switch(ch)
{
case‘E’: eastern++;
break;
case‘W’: western++;
break;
case‘N’: northern++;
break;
case‘S’: southern++;
break;
default:
unknown++;
}
8 How many times are the following loops executed?
(a) X=5; Y=50;
while(X <= Y)
{
X = Y/X;
...........
}
(b) int m=10,n=7;
while (m % n >=0)
{
. .. . . .. . . . . . ..
m =m + 1;
n = n + 2;
...............
}

Ans: (a) Infinite loop
(b) Infinite loop

http://cbsecsnip.in

Page 5 of 13

9 Given the following code fragment:
i = 2;
do {
System.out.println (i);
i += 2;
} while (i < 51);
JOptionPane.showMessageDialog (null, “Thank you”);
Rewrite the above code using a while loop.
Ans: i = 2;
while (i < 51);
{
i += 2;
System.out.println(i);
}
JOptionPane.showMessageDialog (null, “Thank you”);
10 Given the following code fragment:
i = 100;
while (i > 0)
System.out.println (i--)
JOptionPane.showMessageDialog (null, “Thank you”);
Rewrite the above code using a do while loop.
Ans: i = 100;
do{
System.out.println (i);
i--;
}while (i > 0)
JOptionPane.showMessageDialog (null, “Thank you”);
11 Rewrite following while loop into a for loop
int stripes = 0;
while (stripes <= 13) {
if (stripes % 2 == 2)
{
System.out.println(“colour code Red”);
}
else {
System.out.println(“colour code Blue”);
}
System.out.println(“New stripe”);
stripes = stripes + 1;
}
Ans: int stripes = 0;
for(stripes=0; stripes <= 13; stripes++)
{
if (stripes % 2 == 2)
{
System.out.println(“colour code Red”);
}
else
{
System.out.println(“colour code Blue”);
}
System.out.println(“New stripe”);
}

http://cbsecsnip.in

Page 6 of 13

Output and Error Questions

12 Find the output of the following code fragments?
(a) int s = 14;
if (s < 20)
System.out.print("under");
else
System.out.print("Over");
System.out.println("the limit.")
(b) int s = 14;
if(s < 20)
System.outprint("under");
else {
System.out.print("over");
System.out.println("the limit");
}
(c) int s = 94;
if(s < 20){
System.outprint("under");
}
else {
System.out.print("over");
}
System.out.println("the limit");

Ans: (a) Compilation Error
Correct Code
int s = 14;
if (s < 20)
System.out.print("under");
else
System.out.print("Over");
System.out.println("the limit.");
Correct Output-> underthe limit
(b) Compilation Error
Correct Code
int s = 14;
if(s < 20)
System.out.print("under");
else {
System.out.print("over");
System.out.println("the limit");
}
Correct Output-> underthe limit
(c) Output -> Overthe limit
13 What will be the output of following code fragment when the value of ch is
(a) ‘A’ (b) ‘C’ (c) ‘D’ (d) ‘F’?
switch(ch) {
case 'A' : System.out.println("Grade A");
case 'B' : System.out.println("Grade B");
case 'C' : System.out.println("Grade C");
break;
case 'D' : System.out.println("Grade D");

http://cbsecsnip.in

Page 7 of 13

default : System.out.println "Grade F";
}

Ans: (a) When input is A, the output will be as follows:

Grade A
Grade B
Grade C
(b) When input is C, the output will be as follows:
Grade C
(c) When input is D, the output will be as follows:
Grade D
(d) When input is F, the output will be as follows:
Grade F

14 Predict the output of the following code fragment:
int n=0; int j=7; int i=3;
if(i>2) {
n=1;
if(j>4)
n=2;
else
n=3;
}
else {
n=4;
if(j%2 >= 2)
n=5;
else
n=6;
}
JOptionPane.showMessageDialog(null,n);
Ans:

15 Predict the output of the following code fragments:
(a) int i,j,n;
n=0; i=1;
do {
n++; i++;
} while(i<=5);
(b) int i=1,j=0,n=0;
while(i<4) {
for(j=1;j<=i;j++) {
n+=1;
}
i=i+1;
}
System.out.println(n);
(c) int i=3,n=0;

http://cbsecsnip.in

Page 8 of 13

while (i<4) {
n++; i--;
}
System.out.println(n);
(d) int j=1,s=0;
while(j<10) {
System.out.println(j+ "+");
s=s+j;
j=j+j%3;
}
System.out.println("=" +s);
Ans: (a) If System.out.print() is inside while loop output is 12345 and if outside of while loop output is 5
(b) 6
(c) -2147483644
(d) 2+
4+
5+
7+
8+
=27
16 Find out errors, if any:
(a) m=1;
n=0;
for(;m+n<19;++n)
System.out.println("Hello \n");
m=m+10;
(b) while(ctr !=10); {
ctr=1;
sum=sum+a;
ctr=ctr+1;
}
(c) for(a=1, a>10;a=a+1)
{ .......
}
Ans: (a) int m,n;
m=1;
n=0;
for(;m+n<19;++n)
System.out.println("Hello \n");
m=m+10;
(b) while(ctr !=10) {
ctr=1;
sum=sum+a;
ctr=ctr+1;
}
(c) for(a=1; a>10;a=a+1)
{ .......
}

17 Identify the possible error(s) in the following code fragment: Discuss the reason(s) of error(s) and correct the code.
:

http://cbsecsnip.in

Page 9 of 13

f=1;
for(int a=40; (a); a--)
f*=a;
:
s=0;
for(int a=1;a<40/a++)
s+=a;
Ans: :
f=1;
for(int a=40;a>1;a--)
f*=a;
:
s=0;
for(int a=1;a<40;a++)
s+=a;

Type C: Practical/Lab Questions

1 Design an application that performs arithmetic operations (+, -, *, and /). The sample screenshot is being show
below.

Ans: // Addition
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int num1=Integer.parseInt(jTextField1.getText());
int num2=Integer.parseInt(jTextField2.getText());
jTextField3.setText(Integer.toString(Integer.parseInt(jTextField1.getText())+Int
eger.parseInt(jTextField2.getText())));
}
// Substraction
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
int num1=Integer.parseInt(jTextField1.getText());
int num2=Integer.parseInt(jTextField2.getText());
jTextField3.setText(Integer.toString(Integer.parseInt(jTextField1.getText())-
Integer.parseInt(jTextField2.getText())));
}
// Multiply
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
int num1=Integer.parseInt(jTextField1.getText());
int num2=Integer.parseInt(jTextField2.getText());
jTextField3.setText(Integer.toString(Integer.parseInt(jTextField1.getText())*Int
eger.parseInt(jTextField2.getText())));

http://cbsecsnip.in

Page 10 of 13

}
// Division
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
int num1=Integer.parseInt(jTextField1.getText());
int num2=Integer.parseInt(jTextField2.getText());
jTextField3.setText(Integer.toString(Integer.parseInt(jTextField1.getText())/Int
eger.parseInt(jTextField2.getText())));
}
//Clear
jTextField1.setText(null);
jTextField2.setText(null);
jTextField3.setText(null);
2 Practice – if statements. Design a GUI application having interface as shown below:

 In the left half of above interface, two numbers are to be accepted. When the user clicks at button “Which
number is larger ?”, appropriate message gets displayed in the left label.
 In the right half of above interface, a number grade is input. Valid grades are 0-4. Upon clicking the right
push button, a message depicting validity of grade is displayed in right label.

Ans: //larger number
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int num1=Integer.parseInt(jTextField1.getText());
int num2=Integer.parseInt(jTextField2.getText());
if(num1>num2)
{
jLabel4.setText("The first number is larger than the second");
}
else
{
jLabel4.setText("The second number is larger than the first");
}
}
// valid number
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
float num1=Float.parseFloat(jTextField3.getText());
if(num1>=0.0 && num1<=4.0)
{
jLabel8.setText("This is a valid Grade");
}

http://cbsecsnip.in

Page 11 of 13

else
{
jLabel8.setText("This is NOT a valid Grade");
}
}
3 Practice –Loops. Design a GUI application having interface as shown below:

 The loop for the frame should print numbers from lowest to highest on the basis of given two numbers,
upon clicking at “Count” button.

Ans: // loop count
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int num1=Integer.parseInt(jTextField1.getText());
int num2=Integer.parseInt(jTextField2.getText());
String msg="";
if(num1<num2)
{
jLabel1.setText(Integer.toString(num1));
for(int i=num1;i<=num2;i++)
{
msg+=Integer.toString(i)+" ";
}
jLabel1.setText(msg);
}
else
{
jLabel1.setText(Integer.toString(num2));
for(int i=num2;i<=num1;i++)
{
msg+=Integer.toString(i)+" ";
}
jLabel1.setText(msg);
}
}
4 Practice –Select case. Design a GUI application having interface as shown below:

http://cbsecsnip.in

Page 12 of 13

The percentage marks are to be entered in the text box and upon clicking at the button, corresponding grade (as per
following rules) should be displayed in the picture box below command button.
Ans private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int num=Integer.parseInt(jTextField1.getText());

switch(num/10)

{
case 10:
jLabel1.setText("You get an A++");

break;
case 9:
jLabel1.setText("You get an A++");

break;
case 8:
jLabel1.setText("You get an A+");

break;
case 7:
jLabel1.setText("You get an A");

break;
case 6:
jLabel1.setText("You get a B");

break;
case 5:
jLabel1.setText("You get a C");

break;
case 4:
jLabel1.setText("You get a D");

break;
default:
jLabel1.setText("Fail");
}
}

Why Use Java?

  • Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.)
  • It is one of the most popular programming language in the world
  • It is easy to learn and simple to use
  • It is open-source and free
  • It is secure, fast and powerful
  • It has a huge community support (tens of millions of developers)

Get Started

Java Exercises

Java is the foundation for virtually every type of networked application and is the global standard for developing and delivering embedded and mobile applications, games, Web-based content, and enterprise software. With more than 9 million developers worldwide, Java enables you to efficiently develop, deploy and use exciting applications and services.
The best way we learn anything is by practice and exercise questions. Here you have the opportunity to practice the Java programming language concepts by solving the exercises starting from basic to more complex exercises. A sample solution is provided for each exercise. It is recommended to do these exercises by yourself first before checking the solution.
Hope, these exercises help you to improve your Java programming coding skills. Currently, following sections are available, we are working hard to add more exercises .... Happy Coding!
Java GUI Programming
Revision Tour-I
Type A: Very Short/Short Answer Questions
1 What is RAD?
Ans: Rapid Application Development (RAD) describes a method of developing software through the use of pre-
programming tools or wizards. The pre-programmed tools or controls are simply dropped on a screen to visually 
design the interface of application. 
2 What are RAD tools?
Ans: RAD tools are the tool that enables one to create applications in shorter time as compared to conventional language.
3 What is event? What is message? How is it related to an event?
Ans:  An event refers to the occurrence of an activity.
 A message is the information/request sent to the application.
 Each time an event occurs, it causes a message to be sent to the operating system.
4 What are properties? What is property window?
Ans:  Properties are the attributes of graphical controls.
 Properties window displays the editable settings for the currently selected component.
5 What are containers? Give examples.
Ans: A container is a control that can hold other controls within it. For example, JPanel, JFrame, JApplet and JDialog are 
container classes.
6 What are child controls?
Ans: The container control is the one that can contain/hold other controls in it. The contained controls are known as child 
controls.
7 What happens to container control and its child controls if you delete
(i) The container control, 
(ii) A child control
(iii) All child controls?
Ans: (i) If container control is deleted then with container control child control will be also deleted
(ii) If a child control is deleted then the container control will be remain and a child control will be deleted.
(iii) If all child controls is deleted then the container control will be remain and all the child control will be 
deleted.
8 Name the character set supported by java.
Ans: Java uses the Unicode character set.
9 What are keywords? Can keywords be used as identifiers?
Ans:  Keywords are the words that convey a special meaning to the language compiler. These are reserved for 
special purpose.
 Keywords cannot be used as identifiers. 
10 What is an identifier? What is the identifier forming rule of java?
Ans:  Identifiers are user define names for different parts of program.
 Identifier forming rules of java state the following:
1. Identifiers can have alphabets, digits, and underscore and dollar sign characters.
2. They must not be a keyword or Boolean literal or null literal.
3. They must not begin with a digit.
4. They can be of any length.
5. Java is case sensitive i.e., upper-case letters and lower case letters are treated differently.
11 Is java case sensitive? What is meant by the term ‘case sensitive’?
Ans:  Java is case sensitive.
 Case sensitive meant upper-case letters and lower case letters are treated differently.
12 Which of the following are valid identifiers and why/why not?


HERE IS THE PDF AVAILABLE OF VERY SHORT ANSWERS OF SUMITA ARORA BOOK CLASS 12...VERY SOON IT WILL BE AVALILABLE FOR NCERT ALSO.
1.SIMPLY CLICK ON DOWNLOAD BUTTON
2.MY DRIVE WILL BE OPEN .YOU HAVE TO CLICK ON DOWNLOAD BUTTON  WHICH WILL BE PRESENT ON RIGHT SIDE OF THE PAGE.
https://drive.google.com/file/d/1-nu-151LM2yDrFCtp__fKzghBXHP8PgC/view?usp=drivesdk
SOME INFORMATION ABOUT JAVA OPERATORS.

Java Operators

❮ PreviousNext ❯

Java Operators

Operators are used to perform operations on variables and values.
The value is called an operand, while the operation (to be performed between the two operands) is defined by an operator:
OperandOperatorOperand
100+50
In the example below, the numbers 100 and 50 are operands, and the + sign is an operator:

Example

int x = 100 + 50;
Run example »
Although the + operator is often used to add together two values, like in the example above, it can also be used to add together a variable and a value, or a variable and another variable:

Example

int sum1 = 100 + 50;        // 150 (100 + 50)int sum2 = sum1 + 250;      // 400 (150 + 250)int sum3 = sum2 + sum2;     // 800 (400 + 400)
Run example »
Java divides the operators into the following groups:
  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Logical operators
  • Bitwise operators

Arithmetic Operators

Arithmetic operators are used to perform common mathematical operations.
OperatorNameDescriptionExampleTry it
+AdditionAdds together two valuesx + yTry it »
-SubtractionSubtracts one value from anotherx - yTry it »
*MultiplicationMultiplies two valuesx * yTry it »
/DivisionDivides one value from anotherx / yTry it »
%ModulusReturns the division remainderx % yTry it »
++IncrementIncreases the value of a variable by 1++xTry it »
--DecrementDecreases the value of a variable by 1--xTry it »


Java Assignment Operators

Assignment operators are used to assign values to variables.
In the example below, we use the assignment operator (=) to assign the value 10 to a variable called x:

Example

int x = 10;
Try it Yourself »
The addition assignment operator (+=) adds a value to a variable:

Example

int x = 10;
x += 5;
Try it Yourself »
A list of all assignment operators:
OperatorExampleSame AsTry it
=x = 5x = 5Try it »
+=x += 3x = x + 3Try it »
-=x -= 3x = x - 3Try it »
*=x *= 3x = x * 3Try it »
/=x /= 3x = x / 3Try it »
%=x %= 3x = x % 3Try it »
&=x &= 3x = x & 3Try it »
|=x |= 3x = x | 3Try it »
^=x ^= 3x = x ^ 3Try it »
>>=x >>= 3x = x >> 3Try it »
<<=x <<= 3x = x << 3Try it »

Java Comparison Operators

Comparison operators are used to compare two values:
OperatorNameExampleTry it
==Equal tox == yTry it »
!=Not equalx != yTry it »
>Greater thanx > yTry it »
<Less thanx < yTry it »
>=Greater than or equal tox >= yTry it »
<=Less than or equal tox <= yTry it »

Java Logical Operators

Logical operators are used to determine the logic between variables or values:
OperatorNameDescriptionExampleTry it
&& Logical andReturns true if both statements are truex < 5 &&  x < 10Try it »
|| Logical orReturns true if one of the statements is truex < 5 || x < 4Try it »
!Logical notReverse the result, returns false if the result is true!(x < 5 && x < 10)
Related image

No comments:

Post a Comment