How this program compiled and run successfully? [duplicate] - java

This question already has answers here:
Java Label usage [duplicate]
(2 answers)
Closed 6 years ago.
The below program compiled successfully and run without any errors. As per my understanding is should have thrown error in line 4. Can somebody explain?
class Test{
public static void main(String args[]) {
// my favorite website is
http://www.stackoverflow.com/questions/ask
System.out.println("hello world");
}
}

No, it's not an error, "http:" works here as the name of the label and "//" starts the comments which is ignored.

Related

I have an error in my Java program, it is visible on Eclipse IDE [duplicate]

This question already has answers here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
(26 answers)
What is the "String args[]" parameter in the main method?
(18 answers)
Closed 1 year ago.
I have the same error in every code I run on Eclipse IDE or on Notepad, Also it was not earlier. In this code I also having same error which I mentioned below
Code:
package WorkJava;
public class TypeCast{
public static void main(String ar[]) {
int sum=0;
for(int i=0;i<5;i++) {
int a=Integer.parseInt(ar[i]);
sum=sum+a;
}
System.out.println("the sum="+sum);
}
error:-
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
at WorkJava.TypeCast.main(TypeCast.java:1
you are getting the error because you are not passing program arguments so to pass it in eclipse open your class click run on the menu bar the run configurations then argument then in program arguments type 5 integers like the following with spaces.
2 0 4 5 6

Why does java assertion have no effect? [duplicate]

This question already has answers here:
Assertion not working
(3 answers)
Closed 2 years ago.
i have this code:
public class C {
public static void main(String[] args) {
assert (9 < 6):"wrong";
System.out.println("ok");
}
}
I compile with "javac C.java".
I execute with "java C".
It prints "ok". Why does it not work?
Best regards.
as assertions are usually used for debugging they need to be enabled explicitly. hence, add -ea or -enableassertions parameter when starting your program java -ea C

Finding Main Class in "Hello World" program in Java [duplicate]

This question already has answers here:
How to execute a java .class from the command line
(7 answers)
Closed 6 years ago.
I have a seemingly simple program in Java, but when I run it, I get the error:
Error: Could not find or load main class
Here is my code.
public class HelloPrinter
{
public static void main(String[] args)
{
System.out.println("Hello, World!");
}
}
I'm entirely new to the Java language, and I don't know what else to do. My program file is named "hello_world.java", and when attempting to run the program, I type in "java hello_world.java". Am I doing something fundamentally wrong? I've also attempted "java -cp hello_world", but that gave me the same error.
What am I doing wrong?
From what little information you've given, I'd say you're executing it the wrong way.
Make sure you run the following two commands:
javac HelloPrinter.java
java HelloPrinter
The first command compiles your source code into a .class file. The second command executes that class file.

Running .class file from cmd [duplicate]

This question already has answers here:
What does "Could not find or load main class" mean?
(61 answers)
Closed 8 years ago.
Thats code:
package helloWorld;
public class helloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
What's wrong? I am pretty newb.
You need to specify the classpath:
java -cp C:\Users\jklh\Desktop\java_files\helloWorld\bin helloWorld.helloWorld

Website name in java program [duplicate]

This question already has answers here:
My java code has an obvious error. Why does it compile and run?
(2 answers)
Why doesn't putting a random http link in code cause a compilation error? [duplicate]
(2 answers)
Closed 9 years ago.
public class Main
{
public static void main(String[] args)
{
http://google.com
System.out.println("Welcome");
}
}
Why is this code not getting a compilation error? and why did it get a compilation error if we write this url name after System.out.println("Welcome"); ??
Because it's a label followed by a comment.

Categories