Question

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;
}

}
Was it helpful?

Solution 2

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.

OTHER TIPS

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!

No it will not. Just because of the simple reason that ,JRE7 features are not available on JRE6.

It wont even compile if you just even copy paste the code which you implemented on 1.7 to new class of 1.6 ,because you used features which was introduced in 1.7 only

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top