Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java backward compatibility explanation

I have a java class file. I compiled with JVM 7. which I implemented java 1.7 additions like String switchcase, diamond operator. Now I want to run this .class file on java 1.6 JRE. Will it run?

A simple program using string switchcase As I uninstalled 6. Please try it out and give me answer

  import java.util.Scanner;


  public class Classing 
 {

public static void main(String[] args) 
{


    System.out.println("Enter a month to know who you are");
    System.out.println("Jan \n Feb \n Mar \n Apr");

    Scanner scan=new Scanner(System.in);

    String name=scan.nextLine();

    System.out.println(fortune(name.toLowerCase()));

}

public static String fortune(String s)
{


    switch(s)
    {

    case "jan":

        return "Good guy";

   case "feb":

        return "Nice guy";

   case "mar":

      return "Brave guy";

      case "apr":

      return "Super guy";


    }

    return " Month out of option"+s;
}

}
like image 678
seenome Avatar asked Jul 19 '26 01:07

seenome


2 Answers

Java 7's switch on Strings compiles down to the same bytecodes which the Java 6 (and earlier) JRE executes. The same is true of the diamond operator. These are compiler features, not runtime features.

So while I've never tried, I would expect that if you compile code which uses these features using the Java 7 compiler, it should still run on the Java 6 JRE. Of course, if you try to compile that code using the Java 6 compiler, all you will get is a syntax error.

If you really want to know try it and see!

like image 130
Alex D Avatar answered Jul 21 '26 15:07

Alex D


Even though this question has an accepted answer, according to http://www.oracle.com/technetwork/java/javase/compatibility-417013.html#binary

The class file version for Java SE 7 is 51, as per the JVM Specification, because of the invokedynamic byte code introduced by JSR 292. Version 51 class files produced by the Java SE 7 compiler cannot be used in Java SE 6.

like image 37
Scary Wombat Avatar answered Jul 21 '26 14:07

Scary Wombat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!