Output of this code is 10, why? [closed] - java

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.

Related

i am not getting any error in my java code but still it is unable to run? [closed]

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.

data structure is range or tuple [closed]

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 1 year ago.
Improve this question
I am trying to convert the following Java code into swift, but I do not know about this data structure new (int start, int end)[input. length], any guidance would be appraciated.
public override void collection_entries(int[] input)
{
var ranges = new (int start, int end)[input.length];
}
In swift, you should use a Range().
let start = 0
let end = 6
var ranges = Range(start...end)[input.count]

Is it possible to print Fibonacci series in java without using recursion, loop and custom function [closed]

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);
}

Java Program in Eclipse [closed]

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 7 years ago.
Improve this question
How do I make a util class with a method name that is add, and returns the result by adding two numbers.This is in java but in the program eclipse.
This is what I've got:
public class Util {
public static int add(int firstNumber, int secondNumber) {
int first = 2;
int second =3;
int second - Util.add(int, int)
}
}
As a Java novice myself, this is the first Stack Overflow question I've actually been capable of answering. I agree with other commenters that this is probably better answered by reviewing a basic Java textbook of some kind, but here you go:
public class Util {
public static int add(int firstNumber, int secondNumber) {
return firstNumber + secondNumber;
}
}

Writing arithmetic code in Java [closed]

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 7 years ago.
Improve this question
I have the below one line of code:
variableDoubleValue = ((10*variableDoubleA) + (6.25*variableDoubleB) – (5*variableByteC) + 5);
How can I write it in a Java understandable code?
From what I infer from your question, you want a Java-language based variable declaration and assignment.
Well, if this is the case, then the result will be :-
//public class MathemathicalExpression{
// public static void main(String[] args){
// Double A=some_value,B=some_value,Value=0; considering A,B and Value to be of type Double
// Byte C=some_value; considering C to be of type Byte.
Value = 10 * A + 6.25 * B - 5 * C + 5;
// }
//}

Categories

Resources