UrbanPro
true

Learn Java Training from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Java : Compile-time Versus Runtime optimization

S
Surinder Pal Singh
30/12/2020 0 0

While designing and development, one should think in terms of compile-time and run-time.It helps in understanding language basics in a better way.Let's understand this with a question below :

 

What is the difference between line 1 & line 2 in the following code snippet?

 

public class CompileAndRuntimeTest{
     static final int number1 = 5;
     static final int number2 = 6;
     static int number3 = 5;
     static int number4= 6;

public static void main(String[ ] args) {
    int product1 = number1 * number2; //line 1
    int product2 = number3 * number4; //line 2
}
}

Line 1, evaluates the product at compile-time, and Line 2 evaluates the product at runtime. If you use a Java Decompiler (e.g. jd-gui), and decompile the compiled CompileAndRuntimeTest.class file, you will see why , as shown below:

 

public class CompileAndRuntimeTest
{
   static final int number1 = 5;
   static final int number2 = 6;
   static int number3 = 5;
   static int number4 = 6;

public static void main(String[ ] args)
{
   int product1 = 30;//line 1 ---Here is the diff. Decompiled code 
   int product2 = number3 * number4; //line 2
}
}

Constant folding is an optimization technique used by the Java compiler. Since final variables cannot change, they can be optimized. Java Decompiler and javap command are handy tool for inspecting the compiled (i.e. byte code ) code.

 

Question for readers  :)  :

Can you think of scenarios other than code optimization, where inspecting a compiled code is useful?

 

0 Dislike
Follow 2

Please Enter a comment

Submit

Other Lessons for You

What Is Java? Explain The History Of Java
i. Ovierview: Java programming language was originally developed by Sun Microsystems which was initiated by James Gosling and released in 1995 as core component of Sun Microsystems' Java platform (Java...


C, Cpp, Java, Python, Javascript Programming Languages
Learning C is as good as Learning a Natural Language(such as English, Hindi, Telugu, Tamil etc) We use Natural Languages to communicate with humans whereas we use Programming Languages such as C, Cpp,...
V

Vamshi

0 0
0

Constructor Overloading
public class ConstructorOverloading { public ConstructorOverloading(){ System.out.println("default"); } public ConstructorOverloading(String a){ System.out.println("a"); } public ConstructorOverloading(int...
S

Sarthak C.

0 0
0

Inheritance In Java
Inheritance: The process of getting properties and behaviors from one class to another class is called inheritance. Properties: Variables Behaviors: Methods The main purpose of the inheritance...
D

Deleted U.

1 0
0
X

Looking for Java Training Classes?

The best tutors for Java Training Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Java Training with the Best Tutors

The best Tutors for Java Training Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more