Question

In: Computer Science

Explain the difference between a data literal and a variable and how Java interprets numeric literals...

Explain the difference between a data literal and a variable and how Java interprets numeric literals in your code

Solutions

Expert Solution

Q1. Explain the difference between a data literal and a variable.

A variable is a container that holds values that are used in a Java code. Every variable must be declared beforehand to use in program.

A data literal is a constant value in Java created by using a literal representation of it. In other words, literal is a notation for representing a fixed (constant) value.

Note that value stored in variables can change while literal is a constant value

Ex - int A;

A = 1;

Here 'A' is a variable while the constant '10' is a literal

Q2. How Java interprets numeric literals in your code?

1. Integer literal is implicit integer value

By default, the Java compiler treats all integer literals as of type int. For example consider the byte declaration of variable

byte a = 200;

After its compilation, compiler will generate a error message: loss of precision as 200 does not fit in the range of byte which is from -127 to 128.

2. Decimal literal is implicit double value

By default, the Java compiler treats all decimal literals as of type double. For example consider the float declaration of variable

float a = 58.6;

After its compilation, compiler will generate a error message: loss of precision as 58.6 will be treated as a double value by the compiler and conversion from float to double value will lead to loss of precision. Hence, we must use the suffic f or F for forcing conversion.

float a = 58.6f;

3. Representing numeric literals in other bases

  • 0b: indicates a binary literal (available since Java 7).
  • 0: indicates an octal literal.
  • 0x: indicates a hexadecimal literal.
int decimal = 223;
int binary = 0b1101101;
int octal = 0255;
int hexadecimal = 0x48;

4. Underscores can be used in numeric literals

We can use underscores in numeric literals to increase readability of our code and thus leading to a better undertstanding, helpful in debugging of the code. This change was introduced with the Java 7.

5. Conversion from one numeric type to another

int can be converted to long implicitly but not vice versa

int x = 20;

// This is correct as long can hold values of type int
long y = x; 
long y = 20;

// This is not correct as int can't hold values of type long
// Hence compiler will report an error message: loss of precision
int x = y;
long y = 20;

// Explicit conversion is required, called typecasting
int x = (int) y;

Related Solutions

What are the differences between a literal and a variable in Java? Select one: A literal...
What are the differences between a literal and a variable in Java? Select one: A literal is an exact expression representing the symbolic meaning of a value, a variable allows changes to be made to the expression. A literal (or string literal) is a String value at a particular point in your program, a variable is a datatype that the literal can be stored under. A literal is a constant value that is initialised with the keyword final, a variable...
What is the main difference between numeric variable and categorical variable; the main difference between ordinal...
What is the main difference between numeric variable and categorical variable; the main difference between ordinal variable and nominal variable; the main difference between ratio variable and interval variable?
Explain the difference between the terms “variable” and “data.” Include an illustration that demonstrates this difference....
Explain the difference between the terms “variable” and “data.” Include an illustration that demonstrates this difference. For Q2 Recall: “Illustration” = “Example”
A: explain the difference between fixed and variable cost. B: explain the difference between explicit and...
A: explain the difference between fixed and variable cost. B: explain the difference between explicit and implicit cost. C: in the long run in perfect competition economist say that profit =0 but would any firm be involved in that market in the first place?
Explain the difference between fixed and variable costs
Explain the difference between fixed and variable costs
1. Explain the difference between a mediator variable and a moderator variable. Which of the two...
1. Explain the difference between a mediator variable and a moderator variable. Which of the two is more commonly seen in resilience research and why?
Briefly explain the following concepts. a. What is the difference between “variable” and “random variable”? b....
Briefly explain the following concepts. a. What is the difference between “variable” and “random variable”? b. Give two real life examples, one would be appropriate for binomial distribution and the other would for Poisson distribution. c. Explain why binomial distribution is related to the selection of “with replacement” NOT “without replacement”. d. In what situation normal probability rule can be used to obtain binomial probability? How would you evaluate the appropriateness of this rule?
Explain the difference between a non-stationary variable and a stationary variable. Give examples for both of...
Explain the difference between a non-stationary variable and a stationary variable. Give examples for both of them. then Explain the difference between a stationary variable and white noise variable. Give an example of a white noise variable. Why is a white noise variable also a stationary variable? Explain
Explain the difference between variable costing and absorption costing. Describe the limitations of using variable costing....
Explain the difference between variable costing and absorption costing. Describe the limitations of using variable costing. Describe the limitations of using absorption costing. What difference do you notice on the income statement when prepared with absorption costing versus variable costing? "Managerial Accounting"
Explain in your own words the difference between a discrete random variable and continuous variable. Give...
Explain in your own words the difference between a discrete random variable and continuous variable. Give a clear for example  for each that defines that distinct difference.  What does it mean to have a success vs a failure? What the requirements for performing a binomial probability experiment? How do you find the mean and standard deviation of a binomial probability distribution?  discussed probability, what does Do not give a formula - explain the formula itself and use an example to show...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT