Question

In: Computer Science

(c) Explain TWO (2) differences between int and Integer in the context of Java programming using...

(c) Explain TWO (2) differences between int and Integer in the context of Java programming using suitable example,

Solutions

Expert Solution

public class Main
{
public static void main (String[]args)
{
/* 1. First difference is that int can only store int type values
while Integer class can also be used for storing values of string type and
latter that is converted to int type */
  
// Storing integer value as it is a primitive data type
int a1 = 10;

//int a1 = "10" will cause compile time error, since int is a primitive type
// it cannot store different type of values

// Storing String because Integer is a class
// a2 is an object of Integer class
Integer a2 = new Integer ("10");

// Print the sum of integer value of "10"(from a2) and 10(from a1)
int r = a1+a2;
System.out.println ("Sum of "+a1+"(int) and " + a2+"(Integer) is : "+r);
// Output 20
  
  
/* 2. Another difference is that since Integer is a class, Different inbuilt methods can be applied while
int is only used for storing int value */
  
System.out.println("Number of set bits in "+a2+" is : "+Integer.bitCount(a2));
System.out.println("Binary of "+a2+" is : "+Integer.toBinaryString(a2));
System.out.println("Octal of "+a2+" is : "+Integer.toOctalString(a2));
System.out.println("Hexadecimal of "+a2+" is : "+Integer.toHexString(a2));
}
}

OUTPUT :


Related Solutions

Java Language The int t contains an integer between 1 and 50 (inclusive). Write code that...
Java Language The int t contains an integer between 1 and 50 (inclusive). Write code that outputs the number in words and stores the result in the String inwords. For example, if t is 35 then inwords should contain "thirty five". Test Cases Test case #1 Expected result: When t is 2, your code sets inwords to "two" Test case #2 Expected result: When t is 50, your code sets inwords to "fifty" Test case #3 Expected result: When t...
in java Implement a function print2Darray(int[][] array) to print a formatted 4x4 two dimensional integer array....
in java Implement a function print2Darray(int[][] array) to print a formatted 4x4 two dimensional integer array. When the array contains {{10, 15, 30, 40},{15, 5, 8, 2}, {20, 2, 4, 2},{1, 4, 5, 0}}, Your output should look like: {10 15 30 40} {15 5 8 2}{ 20 2 4 2}{ 1450} Now, implement another function print2DList(ArrayList<ArrayList<Integer>> list) to print a formatted 2D list.
Using a while loop. Write a JAVA program that asks a user for an integer between...
Using a while loop. Write a JAVA program that asks a user for an integer between 1 and 9. Using the user input, print out the Fibonaccci series that contains that number of terms. Sample output: How many terms would like printed out for the Fibonacci Sequence? 7 Fibonacci Series of 7 numbers: 0 1 1 2 3 5 8
Describe the differences between high-context and low-context cultures
Describe the differences between high-context and low-context cultures
C programming: Define a function computer_choose that chooses a random integer between 1 and 1000 (inclusive)....
C programming: Define a function computer_choose that chooses a random integer between 1 and 1000 (inclusive). Note: the tests call your function 1000 times to verify that no results are <0 or >1000. Rarely, this can score an incorrect function as correct.
Java Programming Create a program that prompts the user for an integer number and searches for...
Java Programming Create a program that prompts the user for an integer number and searches for it within an array of 10 elements. What is the average number of comparisons required to find an element in the array? Your program should print the number of comparisons required to find the number or determine that the number does not exist. Try finding the first and last numbers stored in the array. Run your program several times before computing the average.
C++ Programming Simply explain what the difference between "including" a header file and using a compiled...
C++ Programming Simply explain what the difference between "including" a header file and using a compiled library file during linking? Where are the C++ header files and compiled C++ library files on your computer?
This is C++ programming Given an int variable k, an int array incompletes that has been...
This is C++ programming Given an int variable k, an int array incompletes that has been declared and initialized, an int variable nIncompletes that contains the number of elements in the array, an int variable studentID that has been initialized, and an int variable numberOfIncompletes, Write code that counts the number of times the value of studentID appears in incompletes and assigns this value to numberOfIncompletes. You may use only k, incompletes, nIncompletes, studentID, and numberOfIncompletes. I tried this code...
Topic is relates to Java programming! Code using java GUI 2. Create the following controls: a)...
Topic is relates to Java programming! Code using java GUI 2. Create the following controls: a) a TextField b) a Button c) a Text d) a Label 3. create the following container and put them the above controls: a) a VBox controller b) a BorderPane controller c) a GridPane
In C++, type a function function(int n, int base) that converts a positive integer x to...
In C++, type a function function(int n, int base) that converts a positive integer x to any base between 2 and 9. The function HAS to do this using a stack, and using methods from below: +isEmpty(): boolean +push(newEntry: ItemType): boolean +pop(): boolean +peek(): ItemType (Also, type a program to test the function). Hint: could use simple iteration continually divides the decimal number by base and keeps track of the remainder by using a stack.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT