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

Spring - Dependency Injection (DI)
Spring - Dependency Injection (DI) DI is a framework which provides loose coupling in code. Here loose coupling means no hard coding of the object. Instead of hard coding, we will be injecting these object...

What Is Applet & Its Life Cycle?
What is Applet & its life cycle?Applet is a Java programme that can be embedded into HTML page.Java Applet runs on the java enables web browsers such as Mozilla & Internet Explorer.Applets are...
I

ICreative Solution

0 0
0

Big Data Hadoop Training
What is Big Data? Big data means really a big data, it is a collection of large datasets that cannot be processed using traditional computing techniques. Big data is not merely a data, rather it has become...

Java8 Filters and collectors
Lets say we have collection of strings and we would like to filter (remove) out certain strings from collection. We could achive the same in java 7 and earlier versions import java.util.ArrayList; import...

CONDITIONAL STATEMENT - IF ELSE
1. IF condition only if is true conditon is required. if(condition){//statements} 2. IF-ELSE condition 1. to check whether the condition will be true or false.syntax of if-else2. only 1 conditionif(condition){//statements}else{//statements} 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