Code exits without printing expected result - java

I cannot figure this out for the life of me. I have a very simple piece of code that keeps terminating on the console window.
public class IsUnique {
public int add(){
return 4;
}
public static void main(String[] args){
IsUnique a = new IsUnique();
a.add();
}
}

public class IsUnique {
public int add(){
return 4;
}
public static void main(String[] args){
IsUnique a = new IsUnique();
a.add();
System.out.println(a);
// Wait until user hits key before quitting
System.in.read();
}
}

Related

I expect infinite loop,but not,why?

code like this
for test two case
one is have volatile keywords ,can stop
other is without volatile,the thread infinite loop
public class VolatileTest extends Thread {
public boolean flag = false;
public static void main(String[] args) throws InterruptedException {
VolatileTest volatileTest = new VolatileTest();
volatileTest.start();
Thread.sleep(1000);
volatileTest.flag = true;
}
#Override
public void run() {
while (!flag) {
System.out.println("=====>");
}
}
}
My mistake. The problem is that you are calling a synchronized method inside your while loop. Try it like this. Stopped will never print unless you redeclare flag as volatile.
public class VolatileTest extends Thread {
public boolean flag = false;
public static void main(String[] args) throws InterruptedException {
VolatileTest volatileTest = new VolatileTest();
volatileTest.start();
Thread.sleep(1000);
volatileTest.flag = true;
System.out.println("flag is now " + flag);
}
#Override
public void run() {
int i = 0;
while (!flag) {
i++;
}
System.out.println("stopped");
}
}

I wrote a method to count positive numbers, but how to call that in main method

public class positiveCount {
private static int countPositive(int[] elems) {
int positive = 0;
for (int i=0;i<elems.length;i++) {
if (elems[i] > 0){
positive++;
}
}
return positive;
}
//This gives me the number of the positive numbers.
public static void main(String[] args) {
}
}
in the main method, i want to enter a list, for example {3,4,5,-2,-3,0},how to call the method positveCount
If the main method is in the same class :
public static void main(String[] args) {
countPositive(new int[] {3,4,5,-2,-3,0});
}
Else (works also for case 1) :
public static void main(String[] args) {
PositiveCount.countPositive(new int[] {3,4,5,-2,-3,0});
}

For_Loop class is not working as expected on eclipse

My For loop is not working and my print statement (i) is showing error in eclipse
Following is the code.
public class For_Loop {
public static void main(String[] args) {
for (int i = 0; i < 10; i++);
System.out.println(i);
}
}
Remove the ; at end of for
public class For_Loop {
public static void main(String[] args) {
for (int i=0;i<10;i++){
System.out.println(i);
}
}
}
Please remove the semicolon from for loop.
public class For_Loop {
public static void main(String[] args) {
for (int i=0;i<10;i++)
System.out.println(i);
}
}

Java - assert does not seem to be executed

I do not understand why the output of this code is -10:
public class Prova {
public void sip(int i){
assert i>=0 : err();
System.out.println(i);
}
public int err(){
...
}
public static void main(String[] args) {
Prova t = new Prova();
t.sip(-10);
}
}
I thought assert would launch err();

Using Objects in a similar way to Variables in Java

Can i use the following code? It's not throwing any error at the Object but at obj.i. Is this a legal way of using an object? Also, how many ways can i create an object other than using the normal syntax obj s = new obj();
public class Test {
static int i;
static Test obj;
obj.i = 10; //am getting a compilation error here "Syntax error on token "i", VariableDeclaratorId expected after this token"
public static void main(String[] args) {
System.out.println(i+" "+ obj);
}
}
You need to place a static block around the obj.i assignment statement for this to work:
public class Test {
static int i;
static Test obj;
static { obj.i = 10; }
public static void main(String[] args) {
System.out.println(i+" "+ obj);
}
}
This is not, you didn't initialize. Furthermore you might not want to use static.
public static void main(String [] args) {
int i = 10;
Test obj = new Test();
obj.setI(i);
System.out.println("my objects I = "+ obj.getI());
}
now in your Test object
public class Test {
private int i;
public void setI(int i) {
this.i = i;
}
public int getI() {
return this.i;
}
}

Categories

Resources