This question already has answers here:
Where is array's length property defined?
(7 answers)
Closed 7 years ago.
I have seen some examples of getting the length of an array using the field length, for example: array.length. I have always used this field but checking the array documentation I did not see that variable. Why is it that the documentation doesn't show it? It only shows a bunch of methods but I can't see the variable length. Is it in another class or what? I have seen questions like this before but the answers are not well explained so I can't understand them.
Because length is not actually a field. The compiler recognizes the identifier specially and translates it to an arraylength instruction rather than a getfield instruction.
Related
This question already has answers here:
Java: Not a statement
(2 answers)
Closed 2 years ago.
I want to know why the following is invalid in Java. Java compiler says that it is not a valid statement.
1+1;
I know the following works.
int i = 1+1;
Please explain why the second one is valid while the first is not. Thanks in advance.
Because you are doing nothing with 1+1. That is not a statement, it's an expression that returns a value that should be stored somewhere, like in the second example you give. If your statements have no effect, they are excluded from the language grammar.
The Java syntax needs the variable to be declared like the following
Class name = value;
You can't create a value without a variable definition and can't create a variable without a name and a class.
This question already has answers here:
Differences between new Integer(123), Integer.valueOf(123) and just 123
(6 answers)
create a new Integer object that holds the value 1?
(2 answers)
Advantage of using new Integer(a) over a
(3 answers)
using new(Integer) versus an int
(3 answers)
Closed 3 years ago.
I am new to learning Java, and I was told to create object variables like this:
Integer a = new Integer(2);
Instead of like this:
Integer a = 2;
Can someone explain why is creating object variables the 2nd way bad?
edit: I am adding this here cause I am getting mixed answers
Which one am I supposed to use and when?
The second approach is actually better, since it will implicitly call Integer.valueOf(). From the docs:
Returns an Integer instance representing the specified int value. If a new Integer instance is not required, this method should generally be used in preference to the constructor Integer(int), as this method is likely to yield significantly better space and time performance by caching frequently requested values. This method will always cache values in the range -128 to 127, inclusive, and may cache other values outside of this range.
(Emphasis mine.)
See also: Autoboxing
This question already has answers here:
While debugging java app what information is shown for a variable in a stack frame [duplicate]
(3 answers)
Closed 4 years ago.
What does 954 mean? I have checked both thread's id and hashcode(), but they don't equal 954.
Also, when using evaluate, there is also a number after #, I think they have the same meaning but still couldn't find out what's the meaning.
Interesting question. I just always took for granted that it is some id that uniquely identifies the object.
Based on that assumption it could for example be the uniqueId() returned by the Java Debugger Interface for an ObjectReference:
https://docs.oracle.com/javase/7/docs/jdk/api/jpda/jdi/
But that is really just an assumption.
This question already has answers here:
What is the id=xxx next to variable entries in the Eclipse Debugger
(3 answers)
Closed 9 years ago.
What means (id=40) in Eclipse near variable PacketNr watch info? What this id is used for?
The ID is just an arbitary number assigned by the eclipse debugger. This can be used to determine whether two references are pointing to the same object or not. So two references to the same object should have a same id. This can be a help while debugging.
That is nothing but a unique object-id, eclipse assigns to objects.
It will just help in debugging.
This question already has answers here:
How to convert number to words in java
(31 answers)
Closed 9 years ago.
I'm having a list of Integer from 1 to 100. If I loop through the list, I wanted to make the output as,
"One"
"Two" .....
"Hundred"
Is there any direct method in Java to obtain the above output?
No such method or class has been provided by JDK.
You can use the code mentioned here or here for reference purpose.
switch case are used to meet that requirement: Here
is source code.
Answer of this question described here: How to convert number to words in java
Officially this is not possible or no standard library available by native Java.
Don't duplicate.
There is none in the official Java libraries. However, the International Components for Unicode project has a RuleBasedNumberFormat with those capabilities. It even has a SPELLOUT constant.