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

Why we need to learn Programming languages?
Language is medium for communication. If two parties like to communicate or exchange the thoughts they must know a language. Language should be understandable by both the Parties. For example A wants to...

Most Important Java Interview Topics
Dear All, I am here to help on java interview preparation for fresher and experienced people, please get in touch with me for further details. All interview preparation will be with unique teaching style...

COMPILATION AND INTERPRETATION
Compilation and Interpertation Process javac (compiler) java(interpreter)high level code - > compile - > bytecode - > interperted - > machine code Bytecode...

Java Collection
Collection: The collection provides an architecture like a list where we can store and manipulate the group of items. Through Collections, we can achieve operations such as searching, sorting, insertion,...

Java inheritance example
Answer these questions Answer the above question
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