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[])
Related
This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 2 months ago.
public class lalak
{
public static void m1(int[] array)
{
array= new int[]{1,2,3,4,5};
System.out.println(array[2]);
}
public static void main(String[] args)
{
int[] array = {1,1,1,1,1};
m1(array);
System.out.println(array[2]);
}
}
why answer is 1, not 3?
i expected the program to print 3 but i got 1 as output.
i thought method would change my original array but it turned out to be false. does anyone know the reason for this?
You're creating a new array which makes the reference to your array in main redundant.
You could either, replace the first line like Jon Skeet said
Or another option would be, to have m1 return an array and assign it to your array in main
public class lalak
{
public static int[] m1(int[] array)
{
array= new int[]{1,2,3,4,5};
System.out.println(array[2]);
}
public static void main(String[] args)
{
int[] array = {1,1,1,1,1};
array = m1(array);
System.out.println(array[2]);
}
}
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 does "void" mean as the return type of a method? [duplicate]
(5 answers)
Closed 5 years ago.
I have done a simple code to reverse a string in Java without using the inbuilt functions. But I have observed that unlike C where we can get a changed string back in the same variable using pointers, which it is not possible in Java due to the absence of pointer concept. So please show me what alternative way can I get back the string in the main function in Java.
class RevFun{
public void revFun(StringBuilder str)
{
for(int i=str.length()-1;i>=0;i--)
{
System.out.println(str.charAt(i));//Here I am able to print it!
}
return;
}
}
class Rev
{
public static void main(String args[])
{
RevFun rev = new RevFun();
StringBuilder str = new StringBuilder("Hello");
System.out.println("Before reversing : "+str);
rev.revFun(str);
System.out.println("After reversing : "+str);//Here what should I do to get the reversed string from RevFun
}
}
give the second method returntype String
public static String handleString(String input){
// modify input
input += " test";
return input;
}
and either use, or assign the (new) value in your main:
public static void main(String[] args){
String a = "hello";
String b = handleString(a);
System.out.println(a);
System.out.println(b);
}
another way is to have your variable on class level:
static String test = "hi";
public static void main(String[] args){
System.out.println(test);
handleString();
System.out.println(test);
}
public static void handleString(){
test += " and bye";
}
both methods have access to the variable, so you won't even need to pass it as a parameter.
In Java Strings are immutable. This means that you can't change them. You can read more here
So if you want to "manipulate" an String you will have to return a new object
This question already has answers here:
public static void main(String arg[ ] ) in java is it fixed?
(9 answers)
Closed 7 years ago.
I have a basic question regarding java.I know it's very basic.However, I want to confirm whether my approach is correct or not.
Generally,we write
public static void main(String[] args)
The compiler starts executing from there on!
I tried it writing in a different way.
class input
{
public static void main(String args)
{
input.main("hello");
System.out.println(args);
}
}
The error i am getting is:
Exception in thread "main" java.lang.NoSuchMethodException:
substrings.main([Ljava.lang.String;)
at java.lang.Class.getMethod(Class.java:1786)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:125)
I have two questions:
What is wrong in having String args instead of String[] args??(Is JVM hardcoded like accepting array arguments?)
What's wrong if i call it as input.main("0") ?
Your main function MUST match the function signature in the main function specification.
This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 7 years ago.
I am trying to print the elements of this array, but it is printing junk characters. What is the problem in this program?
class Demo{
public static void main(String[] args){
int[] x= new int []{5,6,7,8,9,10,11};
{
System.out.println(x);
}
}
}
You need to use the Arrays.toString() method:
System.out.println(Arrays.toString(x));
This gives you a meaningful representation of the data inside your array. The default toString of Java array objects is not a meaningful representation of the data.
public static void main(String args[]) {
int[] x= new int[]{5,6,7,8,9,10,11};
{
System.out.println(Arrays.toString(x));
}
}