Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

In which case a simple java program gives error at Compilation and at runtime ?

 
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
iam a beginner in java.
i just want to know a basic thing that at what situation a simple java program throws error on compilation and/or on runtime ?
iam not talking about the business logic but my question is on its core like syntaxes, or forgetting Main() in entire class like that.

how to differentiate between or predict that it would give error on compilation or on runtime using a notepad and not IDE like ecplise ?
thank you
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a very interesting (odd) question. There are a ton of reasons for compile time errors. You mentioned syntax; that's a big one. Using incorrect spellings of reserved words (import, class, public, private, switch, if...) or using those words out of context; for example, you can't name a variable 'private' or 'class'. Mispelling variables. For example:



That's just scratching the surface. What are you hoping to get from an answer to your question?
 
Ranch Hand
Posts: 61
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most common compiler errors are syntactic in nature... while those at run-time are sementic....

i.e. compiler checks for compliance with the grammer of the language only... while the actual meaning is normally tested at the run-time only... (That's how I try finding errors/ exceptions... or by compiling the program... )

like in the example above from Gregg... beside the error indicated... there is one more error and is "missing return type in method hello()" as there is no retrun type specified in the method definition... and that's a problem according to Java grammer.
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
compile time= grammer/Syntax
run time= real output time

the answer is all ready given by above two post But i am just trying it to put in funny way .
When you will go to purchase a car you will check everything whatever should be there & what every should not be that is COMPILE TIME
But when you will drive the car you get what is exctally missing thats the RUNTIME.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi vinod,

I hope you must be clear now between complile and run time errors..if not check my below examples and understand better.


COMPILE TIME ERROR Example:

public class complietime(){

private String myname = "david";

pubic static void main(String args[]){

System.out.println(MYNAME)
// compile error...since there is no variable called MYNAME
//(only myname is available. remember case sensitiveness)
}

}


RUN TIME ERROR Example:

public class ClassA(){

private ClassB anClassBObject = null;

pubic static void main(String args[]){

anClassBObject.try();
// run time error...since the compiler doesnt know if anClassBObject is instantiated newly.
//only at runtime when it tries to invoke try() method on a Null object it will throw
//NullPointerExcpetion

}

}


public class ClassB(){

private Object emptyObject = null;

pubic void Try() {

System.out.println("hi.......");

}
}


Hope this is clear for you now.


By,
Ibrahim

 
lowercase baba
Posts: 12796
51
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A compile time error is when you don't follow the correct syntax of the language.

for example, in English, you can have a noun, a verb, and an object:

I painted the house.


If i wrote a sentence that had a noun, a verb, another verb, and an object:

I painted swallowed the house.


That violates the rules of the grammar. a verb followed by a verb is not allowed. this would be a compiler error.

Java has VERY SPECIFIC rules on what kinds of things are allowed to follow other things. It's called the Java Language Specification, commonly called the JLS.

For example, the token 'if' must be followed by an open paren character. if you wrote "if Integer X = new Integer();", you would get a compiler error, because you break the rules of the grammar.



a runtime error happens when the code is grammatically correct, but 'something bad' happens at runtime. An obvious example is a division by zero, when the divisor is supplied by the user. In pseudo-code:

get dividend
get divisor
print dividend / divisor

this can be written as legal java code. it is perfectly OK to divide one number by another. However, if the user inputs a 0 for the divisor, that code can't run. You get an error/exception. there is no way for the compiler to know what the user will input, so it can't catch an error like that.
 
We cannot change unless we survive, but we will not survive unless we change. Evolving tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!