0

I am new to java. In java i can see that int and Integer are different. int is primitive and Integer is class. I have done following code.

import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Builder
public class Student {

    private Integer roleNo;
    private String name;
    private String city;


    public static void main(String[] args) {
        // TODO Auto-generated method stub

        List<Student> lstStudent = new LinkedList<Student>();
        lstStudent.add(new Student(111, "aaa", "Moon"));
        lstStudent.add(new Student(333, "ccc", "Sun"));
        lstStudent.add(new Student(222, "bbb", "Jupiter"));


        List<Student> sortedStudents = lstStudent.stream().sorted(Comparator.comparing(Student::getRoleNo))
                                                          .collect(Collectors.toList());

        System.out.println(sortedStudents);

    }

}

I have given breakpoint at sortedstudent list and I can see that I am getting all the values but I am not getting integer value. It shows as 'Integer' ......... why? Am I missing some key concept here?

enter image description here

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
user2746466
  • 361
  • 5
  • 23

1 Answers1

1

This answer here explains quite well what's going on:

Integer is a class, no different from any other in the Java language. Variables of type Integer store references to Integer objects, just as with any other reference (object) type. Integer.parseInt("1") is a call to the static method parseInt from class Integer (note that this method actually returns an int and not an Integer).

To be more specific, Integer is a class with a single field of type int. This class is used where you need an int to be treated like any other object, such as in generic types or situations where you need nullability.

Like @NicholasK says in the comments; if you expand the Integer object in your Debugger you will see that it's an object that contains a single field called value which holds the actual value represented as a primitive int.

Although you can see this for yourself in your IDE, here is a screenshot to provide more context for all those that might stumble upon this having the same question:

enter image description here

Read more about Java Debugging with Eclipse.

Community
  • 1
  • 1
Matthew
  • 1,905
  • 3
  • 19
  • 26
  • what is this value exactly ? I can see there is value field for string as well. – user2746466 Jun 02 '19 at 03:38
  • @user2746466 This is specific to IntelliJ IDEA which I am using. The String field is just the main method parameter `args`. As I've already stated in the answer the `int` value of your `Integer` object can be viewed by expanding the object entry in your *Variables* window when debugging. – Matthew Jun 02 '19 at 10:08
  • @user2746466 I've provided an additional resource at the end of the answer for you to read if you want to know more about debugging with Eclipse in general. I believe your question has been answered, and although you can ask follow-up questions connected with the original question please read [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) for more information. – Matthew Jun 02 '19 at 10:10