Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I know that primitive types called by value(a copy of value has been sent)
My code:
static int a = 5;
public static void main(String[] args) {
System.out.println("a= " + a);
setA();
System.out.println("a= " + a); //Why is not 5?
setA2(a);
System.out.println("a= " + a); //Why is not 7?
}
public static int getSeven() {
return 7;
}
public static void setA() {
a = 6;
}
public static void setA2(int n) {
n = getSeven();
}
Output:
a= 5
a= 6
a= 6
Why other two output is 6 again?
Why a is changed?
a is not an object!
The second output is 6 since you set it to 6 by the preceding row (function setA()). setA() is setting a primitive outside of the function but in the class, so this is also available in main().
The third output is 6 since you set the primitive n instead of a. It is a primitive so it has no reference to a anymore.
a is global variable.
setA() will set the value of a as 6. So every where the value of a will be six.
getSeven() is returning 7 but you are not capturing it. So a will remain as it is.
try it like this a=getSeven();
setA2(int n)is assigning n to 7 but not to a.
try it like this
public static void setA2(int n) {
a = getSeven();
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 months ago.
Improve this question
I don't know why my defined function is not working smoothly.
in this I am trying to build a function to convert a binary number to a decimal number
public class binarrytodec {
public static void bintodec(int n){
int p=0;
int dec=0;
while(n>0){
int ld=n%10;
dec = dec + (ld*(int)Math.pow(2, p));
p++;
n=n%10;
}
System.out.print(dec);
}
public static void main(String args[]) {
bintodec(1110001);
}
}
% is not division, it's a remainder operation - it shows how much is left after integer division. 1 % 10 == 1. Thus your n is never becoming 0, and your code enters an infinite loop.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Why is this giving me 10 as output, rather than an error?
public class A {
static int a = m1();
static int m1(){
return 10;
}
public static void main(String args[]) {
A a1 = null;
System.out.println(a1.a);
}
}
Because compiler is so intelligent here,it basically replaces
System.out.println(a1.a);
with
System.out.println(A.a); //The name of your class 'A'
Because a is a static variable, so the reference to A a1 isn't dereferenced. You might want to write A.a instead to make the code more intuitive.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Trying to call a method within a method.
public static void main(String[] args) {
int sq,cu=0;
//user input 1
sq=Integer.parseInt(JOptionPane.showInputDialog(
"Enter value to be squared"));
//user input 2
cu=Integer.parseInt(JOptionPane.showInputDialog(
"Enter value to be cubed"));
//results
JOptionPane.showMessageDialog(null, sqd(sq));
JOptionPane.showMessageDialog(null, cbd(cu));
}
public static String sqd(int sq){
int sqd=sq*sq;
//sq computation
return sq+" squared is "+sqd;
}
public static String cbd(int cu,int sqd){
int cbd;
cbd=sqd*cu;
//cu computation
return cu+" cubed is "+cbd;
} }
calling sqd value in cbd, but
JOptionPane.showMessageDialog(null, cbd(cu));
prevents me from doing so, it always gives me an error when I run it.
your cbd(int cu, int sqd) method needs 2 input parameters , you are calling it with only one parameter cbd(cu)
two choices :
1 - rewrite your cbd method with 1 parameter then you can call it using cbd(cu);
public static String cbd(int cu){
return cu + " cubed is " + (cu*cu*cu);
}
2 - write it's second parameter while you are using it :
cbd(cu,cu*cu);
Your cbd method, as it's currently defined, takes two arguments - cu and sqd. If you want to keep the way your main calls it, you need to rewrite it with just one argument:
public static String cbd(int cu){
int cbd = cu * cu * cu;
return cu + " cubed is " + cbd;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Why aren't variables I modify inside myMethod are not modified outside that scope?
public class TestLoop {
public static void main(String[] args) {
myMethod(0, 3);
}
static void myMethod(int i, int j) {
System.out.println("i for:" + i + " j:" + j);
if (i == j)
return;
else {
myMethod(i + 1, j);
System.out.println("after myMethod Call for: i:" + i + " j: " + j);
}
System.out.println("outside i for: " + i);
}
}
Each time you call myMethod(i + 1, j), new local variables i and j are created on the stack and initialized to the values passed to them by the caller.
When each execution of myMethod() returns, the local variables i and j get out of scope. You return to the previous myMethod() execution, which has its own local variables i and j having their own values.
Finally, when the call stack returns to the original myMethod(0,3) call, that execution has local variables i and j with values 0 and 3.
The values of i and j never change. There are just multiple local variables named i and j, each one having a limited scope.
The JLS - Chapter 14. Blocks and Statements contains the answer for your question:
The break (§14.15), continue (§14.16), and return (§14.17) statements cause a transfer of control that may prevent normal completion of statements that contain them.
The control is returned back to the statement where the method was called from.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have been trying various methods. However I cannot find out whether it is possible to print Fibonacci series in java without using recursion, loop and custom functions. If yes then how?
You can use the scheduled exector pool. It's not technically a loop, though it is using the repeating thread to behave like a loop.
private static volatile int currentNum = 1;
private static volatile int previousNum = 0;
public static void main(String[] args) {
ScheduledThreadPoolExecutor timer = new ScheduledThreadPoolExecutor(1);
timer.scheduleWithFixedDelay(() -> {
System.out.println(currentNum);
int temp = currentNum;
currentNum += previousNum;
if (currentNum < 0) {
// overflow
timer.shutdown();
}
previousNum = temp;
}, 0, 1, TimeUnit.MILLISECONDS);
}