This question already has an answer here:
Any difference between `var x` and `int x`? [duplicate]
(1 answer)
Closed 1 year ago.
I am stuck on how to use var. Is there any difference between this two?
var input = (String) result.get("some field from DB");
String input = (String) result.get("some field from DB");
In java 10 and above when you use var you are allowing the compiler to attempt to decide what the variables type should be. In your example your casting a String so it will give that variable type String.
In your second example you are specifically telling the compiler that your variable is of type String.
The second is safer if you know what your variable type will be as it leaves no confusion down to the compile.
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 an answer here:
Java Reflection: "java.lang.NoSuchMethodException"
(1 answer)
Closed 5 years ago.
ResultSet data_result = dao.getDetails();
Method resultset_method;
resultset_method = data_result.getClass().getMethod("getInt", Integer.class);
it is giving error:
java.lang.NoSuchMethodException: org.apache.commons.dbcp.DelegatingResultSet.getInt(java.lang.Integer)
The ResultSet's getInt method has two overloads, one that takes an int for the index and one that takes a String for a column name. You're attempting to retrieve a getInt() method, which indeed does not exist.
You need to supply the types that the method takes - eg, if you were going by name,
resultset_method = data_result.getClass().getMethod("getInt", String.class);
This question already has answers here:
Difference between int[] array and int array[]
(26 answers)
Closed 7 years ago.
I've seen examples such as:
Type arrayname[] = new Type[];
also as written as:
Type[] arrayname = new Type[]
I am quite confused about such expressions!
Where exactly should I put the []?
Any of the above are allowed. All produce the same bytecode. JLS-10.2 Array Variables says (in part)
The [] may appear as part of the type at the beginning of the declaration, or as part of the declarator for a particular variable, or both.
This question already has answers here:
Difference between casting to String and String.valueOf
(8 answers)
Closed 9 years ago.
This morning I found an interesting question --- cast object to string check if valid and I found there are two types of answers. One is to cast an object to String, the other one is to get the string representation of that object instead (eg. using String.valueOf() or toString()). My questions are: what is the best practice? what is the difference between them?
Before I asked this questions, I found several existing questions which are relevant, but I didn't find one which answers my question. Please forgive me if I missed the important one and hope you don't mind pointing me to the answers.
Thank you,
If the Object is not a String, a cast will throw a ClassCastException at runtime. For example:
Object o = new Object();
String s = (String) o; //Exception here
The difference between the other two solutions (toString vs. String.valueOf) is in the case of a null object. toString will throw an exception whereas String.valueOf() will simply return "null":
Object o = null;
String s = String.valueOf(o); //s = "null";
String t = o.toString(); //NullPointerException