I am trying to compile my Java program, which invoves a switch statement with a string as the Case object. I am getting the following error:
Error: Cannot switch on a value of type java.lang.String. Only convertible int values or enum constants are permitted
I understand that older versions of Java do not let you switch on a string, but I thought I had downloaded Version 8 Build 251. I am running Mac OS10.14.6.
The "About" and "System Properties" of Dr. Java are given below:
DrJava Version : drjava-20120818-r5686
DrJava Build Time: 20120818-0422
DrJava Configuration File: /Users/berkeley/.drjava
Used memory: about 50.95 megabytes
Free memory: about 32.05 megabytes
Total memory: about 83 megabytes
Total memory can expand to: about 1.78 gigabytes
apple.laf.useScreenMenuBar true
awt.toolkit sun.lwawt.macosx.LWCToolkit
drjava.debug.port 50922
file.encoding UTF-8
file.encoding.pkg sun.io
file.separator /
ftp.nonProxyHosts local|.local|169.254/16|.169.254/16
gopherProxySet false
http.nonProxyHosts local|.local|169.254/16|.169.254/16
java.awt.graphicsenv sun.awt.CGraphicsEnvironment
java.awt.printerjob sun.lwawt.macosx.CPrinterJob
java.class.path /Applications/DrJava.app/Contents/Resources/Java/drjava.jar
java.class.version 52.0
java.endorsed.dirs /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/lib/endorsed
java.ext.dirs /Users/berkeley/Library/Java/Extensions:/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/lib/ext:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java
java.home /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
java.io.tmpdir /var/folders/3v/4sx9hl_s2jz8g88d_y0sr4pc0000gn/T/
java.library.path /Users/berkeley/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.
java.rmi.server.hostname 127.0.0.1
java.runtime.name Java(TM) SE Runtime Environment
java.runtime.version 1.8.0_251-b08
java.specification.name Java Platform API Specification
java.specification.vendor Oracle Corporation
java.specification.version 1.8
java.vendor Oracle Corporation
java.vendor.url http://java.oracle.com/
java.vendor.url.bug http://bugreport.sun.com/bugreport/
java.version 1.8.0_251
java.vm.info mixed mode
java.vm.name Java HotSpot(TM) 64-Bit Server VM
java.vm.specification.name Java Virtual Machine Specification
java.vm.specification.vendor Oracle Corporation
java.vm.specification.version 1.8
java.vm.vendor Oracle Corporation
java.vm.version 25.251-b08
line.separator
os.arch x86_64
os.name Mac OS X
os.version 10.14.6
path.separator :
socksNonProxyHosts local|.local|169.254/16|.169.254/16
sun.arch.data.model 64
sun.awt.enableExtraMouseButtons true
sun.boot.class.path /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/lib/resources.jar:/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/lib/rt.jar:/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/lib/sunrsasign.jar:/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/lib/jsse.jar:/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/lib/jce.jar:/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/lib/charsets.jar:/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/lib/jfr.jar:/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/classes
sun.boot.library.path /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/lib
sun.cpu.endian little
sun.cpu.isalist
sun.font.fontmanager sun.font.CFontManager
sun.io.unicode.encoding UnicodeBig
sun.java.command /Applications/DrJava.app/Contents/Resources/Java/drjava.jar
sun.java.launcher SUN_STANDARD
sun.jnu.encoding UTF-8
sun.management.compiler HotSpot 64-Bit Tiered Compilers
sun.os.patch.level unknown
user.country US
user.dir /Applications/DrJava.app/Contents/Resources/Java
user.home /Users/berkeley
user.language en
user.name berkeley
user.timezone America/Chicago
I just saw this support request. I somehow missed seeing it earlier. You are running an ancient version of DrJava (from 2012!) that I don't think is compatible with Java 8. I also do not what compiler you are using. DrJava searches for all the Java compiler it can find in the usual places on your platform, but I doubt that the 2012 edition will load a Java 8 compiler (which should be located in the tools.jar file in your Java JDK 8 installation. I suspect DrJava is finding an older compiler somewhere (or using an old Eclipse (Java 6?) open source compiler that was bundled with old versions of DrJava). You can get the latest official release of the DrJava jar file (labeled "Beta" but that was over a year ago) at drjava.org, or an even more recent build at www.cs.rice.edu/~javaplt/drjavarice. Both are Java 8 compatible. I am running the drjavarice version (DrJava Version : drjava-20200303-210720) on top of the Amazon Corretto 8 JDK which includes a Java 8 compiler. The open source releases of Java like Amazon Corretto 8 include an open source compier, which is now bundled with DrJava for use in case you are running a Java JRE (no compiler include) release instead of a JDK release.
My coding style matches Java 6 rather than Java 8 so I have never tried switching on a String. Now that I know such an option exists, I doubt I will ever use it. Why? I have no idea how it is implemented; AFAIK it is up to the compiler writer. If the implementation uses linear matching (basically an "if-then-else" ladder), it will very run slowly if you have very many switch cases. The implementation could use a hash-table (for near constant time performance in the absence of pathologies), but the hash table lookup overhead is reasonably large (including computing the hash code of the String switch object, unless you intern() all of your Strings [which performs the hashing when you execute the intern() operation]. According to Oracle Java 8 documentation, "The Java compiler generates generally more efficient bytecode from switch statements that use String objects than from chained if-then-else statements." so if the switch statement appears in code that is not performance critical, you should be OK.