Difference between two given generic statements [duplicate] - java

This question already has answers here:
What is the point of the diamond operator (<>) in Java?
(7 answers)
Why diamond operator is used for Type Inference in Java 7?
(6 answers)
Closed 5 years ago.
Gen<Integer> y=new Gen(2); // Line1
Integer x=y.getOb(); //Line 2
Gen<Integer> y1=new Gen<>(2); // Line3
Integer x1=y1.getOb();//Line4
class Gen<T>
{
T val;
Gen(T ob)
{
val=ob;
}
T getOb()
{
return val;
}
}
I am not able to find any difference between y and y1 objects. Please help me in understanding this.
FYI- It is getting compiled and giving right output.

Related

concatenation and displaying a string [duplicate]

This question already has answers here:
Does concatenating strings in Java always lead to new strings being created in memory?
(3 answers)
Converting String to "Character" array in Java
(14 answers)
String to char array Java
(1 answer)
Closed 3 months ago.
a string created with String class can not be modified.
but when we use the += operator does it mean that the original string change?
exp:
String ch="hello"; ch+= "world";
another question:
why we don't use these instructions to display the string ?
for (int i=0;i<ch.length();i++) {System.out.println(ch[i]); }
i tried this
for (int i=0;i<ch.length();i++) {System.out.println(ch.charAt(i)); }
why it is not similar to
for (int i=0;i<ch.length();i++) {System.out.println(ch[i]); }

What is the difference between int parseInt(String s) and Integer.valueOf(String s)? Also What is radix with a numerical parameter {10 or etc} [duplicate]

This question already has answers here:
Different between parseInt() and valueOf() in java?
(11 answers)
Integer.valueOf() vs. Integer.parseInt() [duplicate]
(5 answers)
What is the radix parameter in Java, and how does it work?
(6 answers)
Closed 3 years ago.
I have used these two in Java and BlueJ but I am not sure where the difference between these two lies. Also in my book in the description given for parseInt there is a mention of radix 10. What exactly is radix 10?
The two methods are almost the same in terms of logic.
The difference is that valueOf returns an Integer and parseInt returns an int.
You can see that both call parseInt(s, 10), which means they treat the input String as a number of radix (base) 10 (i.e. a decimal number).
public static Integer valueOf(String s) throws NumberFormatException {
return Integer.valueOf(parseInt(s, 10));
}
public static int parseInt(String s) throws NumberFormatException {
return parseInt(s,10);
}

Is it possible to concatenate assignments in Java? [duplicate]

This question already has answers here:
Initializing multiple variables to the same value in Java
(7 answers)
Closed 4 years ago.
Is it possible to assign to two variables in one statement?
E.g.
this.edge = this.side = null;
Adding one line will not cost you much, just use:
this.edge = null;
this.side = null;

what is the difference between following equality checks? [duplicate]

This question already has answers here:
Java: Integer equals vs. ==
(7 answers)
Why do we use autoboxing and unboxing in Java?
(10 answers)
How can I properly compare two Integers in Java?
(10 answers)
Integer == int allowed in java
(6 answers)
Closed 4 years ago.
System.out.println(5 == new Integer(5)) output = true
Integer i31 = 2;
Integer i32 = new Integer(2);
System.out.println(i31 == i32); output = false
It rather seems since we are in function scope. hence different output.
Unable to grasp what can be different.

A static method parseInt(char[]) that converts an array of numeric characters to an int value [duplicate]

This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 7 years ago.
Here's my code
public static int[] parseInt(char[] myChar)
{
int[] myInt = new int[myChar.length];
for(int x = 0; x < myChar.length; x++)
{
myInt[x] = (int)myChar[x];
}
return myInt;
}
When I System.out.println(object.parseInt(myChar)); with values {'1','2','3'} I would expect it to print myInt with the corresponding unicode values for {'1','2','3'}.
Instead I get [I#15db9742
Any ideas on what I'm doing wrong?
That because you are printing the object, hence java shows:
[I - the name of the type (in this case it is one dimensional [ array of int)
# - joins the string together
15db9742the hashcode of the object.
the memory address by default. You can use:
System.out.println(Arrays.asList(parseInt(array)));

Categories