Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1) Write a program that does the following: Create seven variables, one for each

ID: 662692 • Letter: 1

Question

1) Write a program that does the following: Create seven variables, one for each of the primitive number types in Java, and initialize each variable with any appropriate value. Print out the name of each variable and its value. Modify the value of each variable with an assignment statement and print out the names of the variables and their new values.

Next, create seven constants, one for each of the primitive number types in Java. Print the name of the constant and its value.

What happens if you try to assign a value to a constant?

Explanation / Answer

import java.util.Scanner;

public class sevenVariables {
  
  
public static void constants()
{
// you can use "final" keyword to declare constants
final byte var_byte=0;
final short var_short=0;
final int var_int=10;
final long var_long=10;
final double var_double=10.3;
final float var_float=10f;
final boolean var_boolean=true;
System.out.println("var_byte= "+var_byte);
System.out.println("var_short= "+var_short);
System.out.println("var_int= "+var_int);
System.out.println("var_long= "+var_long);
System.out.println("var_double= "+var_double);
System.out.println("var_float= "+var_float);
System.out.println("var_boolean= "+var_boolean);
}
public static void main(String[] args)
{
//initialization
byte var_byte=0;
short var_short=0;
int var_int=10;
long var_long=10;
double var_double=10.3;
float var_float=10f;
boolean var_boolean=true;
//printing variable names and values
System.out.println("var_byte= "+var_byte);
System.out.println("var_short= "+var_short);
System.out.println("var_int= "+var_int);
System.out.println("var_long= "+var_long);
System.out.println("var_double= "+var_double);
System.out.println("var_float= "+var_float);
System.out.println("var_boolean= "+var_boolean);
//using assignment to change values of variables...you can use different one
byte x=1;
short y=0;
var_byte=(byte)(var_byte|x);
var_short=(short)(var_short^y);
var_int=var_int/2;
var_long=var_long/2;
var_double=var_double/2;
var_float=var_float/2;
var_boolean=!var_boolean;
//printing changed values....
System.out.println("var_byte= "+var_byte);
System.out.println("var_short= "+var_short);
System.out.println("var_int= "+var_int);
System.out.println("var_long= "+var_long);
System.out.println("var_double= "+var_double);
System.out.println("var_float= "+var_float);
System.out.println("var_boolean= "+var_boolean);
  
//constants
constants();

  
}

}

"Final" keyword is used to declare constant variable.

you can use "public static final" to declare constants too...depending upon your requirements of accessiblity.

the main thing is the magic "fianl" keyword.

What happens if you try to assign a value to a constant?

When we declare a variable to be "final" we are telling Java that we will NOT allow the variable