3.2 CJF-B-Ejercicio-01-VariableHandling PDF
3.2 CJF-B-Ejercicio-01-VariableHandling PDF
3.2 CJF-B-Ejercicio-01-VariableHandling PDF
mx
Create a program to practice creating variables in Java. In the end we
should observe the following:
www.globalmentoring.com.mx
We close any project that is open:
www.globalmentoring.com.mx
We create a new project VariableHandling:
www.globalmentoring.com.mx
We select the Java category and a Java Application:
www.globalmentoring.com.mx
( )
We assign a name and a path for our new project as show
below:
www.globalmentoring.com.mx
We will create a new Java class:
www.globalmentoring.com.mx
( )
We assign the name VariablesHandlingTest to our new class, and assign to a new
package named variables. Remember that a package is like a folder. We will cover
the topic of packages later in the course. Click on finish:
www.globalmentoring.com.mx
( )
This is the result of the created class. We are going to modify this class with the
following code:
www.globalmentoring.com.mx
File VariablesHandlingTest.java: Click to download
package variables;
//Byte Variables
byte b1 = 10;//decimal value of 10
//Literal en hexadecimal starts with 0x
byte b2 = 0xa;//0xa is hexadecimal, equals to 10 in decimal
System.out.println("byte1 value:" + b1);
System.out.println("byte2 value:" + b2);
System.out.println("");//prints a new line too
www.globalmentoring.com.mx
File VariablesHandlingTest.java: Click to download
//Short Variables
short s1 = 2;
System.out.println("short value:" + s1);
System.out.println("");//prints a new line
//int Variables
int decimal = 100;
int octal = 0144;//An octal value starts with 0
int hexa = 0x64;//An hexadecimal value starts with 0x or OX
System.out.println("int decimal value:" + decimal);//printls 100
System.out.println("int octal value:" + octal);//prints 100 too
System.out.println("int hexadecimal value:" + hexa);//prints 100 too
System.out.println();
www.globalmentoring.com.mx
File VariablesHandlingTest.java: Click to download
//long variables
long long1 = 10;//by default a literal is of type int
long long2 = 20L;//with the sufix l or L the literal is converted to long
System.out.println("long1 value:" + long1);
System.out.println("long2 value:" + long2);
System.out.println();
//Variables float
float f1 = 15;//by default a float literal is of type double
float f2 = 22.3F;//with the sufix f or F the literal is converted to float
System.out.println("float1 value:" + f1);
System.out.println("float2 value:" + f2);
System.out.println();
//double Variables
double d1 = 11.0;//by default a float literal is of type double
double d2 = 30.15D;//with the sufix D, converts a literal to double
System.out.println("double1 value:" + d1);
System.out.println("double2 value:" + d2);
System.out.println();
}
}
www.globalmentoring.com.mx
We execute our project. We give right click -> Run:
www.globalmentoring.com.mx
( )
The result is:
www.globalmentoring.com.mx
• Try creating more variables and verify the result.
www.globalmentoring.com.mx
• With this exercise we have put into practice the creation of
variables, which are the basis for storing information
temporarily of our program.
• http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
www.globalmentoring.com.mx
Author: Ubaldo Acosta
www.globalmentoring.com.mx