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

Class and Objects in Java
Class is a template or a blueprint which is used to describe an object. On other hand Object is a reference of a class which follows all the stuff written inside the class. How about taking the whole tour in the following video

Session Tracking In Java Servlets
Session Tracking: HTTP is a stateless protocol. Each request is independent of the previous one. However, in some applications, it is necessary to save state information so that information can be collected...

Java : Command Line Arguments
The parameters which are provided to the program at the command line. Eg:d:\>java a 10 20 30 Here,“java” is an interpreter, “a” is filename,10,20,30 are arguments passed to...
S

Svg Reddy

0 0
0

How can everyone prepare to clear any Java interview?
Java interview your java should be much strong then J2EE. core java and Advance java is the basic foundation for Interview. Some of the topic about which you should know before going for a java interview...

Method Overloading vs Method Overriding
1. Method Overloading: Method overloading means a same method with same name can have different implementations/body by changing one of the follwing thing: 1) Number of Parameters 2) Order of Parameters 3)...
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