This question already has answers here:
Where did the 'M' go? [duplicate]
(4 answers)
Closed 5 years ago.
I checked the following code found that instead of printing A123 it is priting 123 .
Can some one explain what's going on here.
public class Test{
public static void main(String[] args) {
StringBuffer sb = null;
sb = new StringBuffer('A');
sb.append('1');
sb.append('2');
sb.append('3');
System.out.println(sb);//Printing 123
}
You're calling the constructor that specifys the capacity. Try this
sb = new StringBuffer("A");
You encountered int to char conversion.
You are invoking the constructor StringBuffer(int capacity).
public StringBuffer(int capacity) {
super(capacity);
}
Since you passing the char, it converted to int value (ASCII value) and taking as capacity.
Related
This question already has answers here:
print current date in java
(9 answers)
Method undefined for type Java
(1 answer)
Closed 6 months ago.
public class StringBuilder {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
for(char ch='a'; ch<='z'; ch++){
sb.append(ch);
}
System.out.println(sb);
}
}
This question already has answers here:
Java arrays printing out weird numbers and text [duplicate]
(10 answers)
Closed 1 year ago.
I'm sure this is dumb, I'm just learning to code and trying to follow instructions on how to make this method that multiplies doubles into an array. The code compiles but does not return my array, it just gives me [D#76ed5528 which I'm assuming is the memory address of the array?
public class Ex1 {
public static void main(String[] args) {
Ex1 exone = new Ex1();
double[] bob = exone.square(2, 6, 9, 8);
System.out.println(bob);
}
public double[] square(int a, int b, int c, int d) {
double[] result = {a*a, b*b, c*c, d*d};
return result;
}
}
Yes #Drunkasaurus, the [D#76ed5528 you mentioned in is indeed the hash for the array in memory. In order to print the values in your array you have a few options including:
Using the Arrays.toString() method: https://stackoverflow.com/a/29140403/10152919
Using a simple loop:
https://stackoverflow.com/a/409795/10152919
Also, no question is a dumb question if you can learn from it 🙂
This is because you are directly trying to return the result array.
Try to iterate over the array.
For ex - https://www.geeksforgeeks.org/iterating-arrays-java/
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
This question may be redundant but I really don't understand why the following code throws: Exception in thread "main" java.lang.NullPointerException
public class NewClass {
static StringBuilder SB;
public static void main(String[] args) {
SB.append("Tesing");
System.out.println(SB);
}
}
SB = new StringBuilder();
You missed this part!
You haven't assigned the SB (shouldn't be capitalised btw) variable, so it is still null when you attempt call a method on it.
public class NewClass {
// Assignment added below
static StringBuilder sb = new StringBuilder();
public static void main(String[] args) {
sb.append("Tesing");
System.out.println(sb);
}
}
You didn't initialize your StringBuilder. Change it to
static StringBuilder SB = new StringBuilder(); and it should work.
This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 6 years ago.
I know that String is immutable and it's value can't be changed, but why does the value of the below StringBuffer doesn't change when sent like a method parameter. From my understanding it should have changed with the new value "bb". Thank you for your help.
class Ideone {
public static void main (String[] args) {
String s = "aa";
StringBuffer sb = new StringBuffer("aa");
modify(s, "bb");
modify2(sb, "bb");
System.out.println(s);
System.out.println(sb);
}
public static void modify(String s, String ss) {
s = ss;
}
public static void modify2(StringBuffer sb, String ss) {
sb = new StringBuffer(ss);
}
}
The universal rule in Java is that you cannot change the reference of an object passed into a method, but you can change its contents.
public static void modify2(StringBuffer sb, String ss){
This method takes a copy of a reference to a StringBuffer. Changing that reference to point to an object has no effect whatsoever on the original object. But if you implemented it as
sb.clear();
sb.append(ss);
then it would work.
Again, the rule is that reassigning an object passed into a method with = does nothing to the original object, but you can change the contents of that object just fine.
This question already has answers here:
Java arrays printing out weird numbers and text [duplicate]
(10 answers)
Closed 8 years ago.
and thanks for reading this question from a beginner.
I tried to run this code on the command:
public class a{
public static void main(String[] args){
String[] s = args;
System.out.println(s);
}
}
and the following input: "java a this is string"
gives me:
"[Ljava.lang.String;#65d4ab0e" as an output.
What should I do to get "this is string" as output?
You are printing the array object itself, not the content of the array.
The hashcode value [Ljava.lang.String;#65d4ab0e indicates that, s is an array and it's has String values. The leading [ says, the object you have printed is an array.
public class a{
public static void main(String[] args){
String[] s = args;
System.out.println(Arrays.toString(s));
}
}
Use Arrays.toString(s)
Take a look at http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#toString(java.lang.Object[])