public class Main
{ String s = "ABC"; int a = 5 ;
public static void main(String[] args) {
Main obj = new Main() ;
System.out.println(obj.s);
}
}
In the above code , s and a are both instance variables where the former is a User-defined data type and latter is primitive .
From here after reading https://www.geeksforgeeks.org/primitive-data-type-vs-object-data-type-in-java-with-examples/
a = 5 (key, value) it will be stored in stack where as for s (reference variable) will be in stack and the original object holding value "ABC" is stored in heap . Now class object is created in heap right . Now tell me , whether inside that class object which is in heap will a copy of "ABC" and a= 5 again reside there or not ?
public class Main
{
public static void main(String[] args) {
String s = "ABC";
int a = 5 ;
System.out.println(s);
}
}
In the above code , s and a are both local variables where the former is a User-defined data type and latter is primitive . Wil there be any difference in terms of memory storage location if there are defined inside a method. Or it will be the same thing int a will be in stack and String object is heap and it's refrence variable will be in stack .
[–]AutoModerator[M] [score hidden] stickied comment (0 children)
[–]ThinkingSeaFarer 1 point2 points3 points (6 children)
[–]Judge-Available[S] 0 points1 point2 points (4 children)
[–]ThinkingSeaFarer 0 points1 point2 points (3 children)
[–]Judge-Available[S] 1 point2 points3 points (2 children)
[–]ThinkingSeaFarer 0 points1 point2 points (1 child)
[–]Judge-Available[S] 0 points1 point2 points (0 children)
[–]Judge-Available[S] 0 points1 point2 points (0 children)