tldr: I downgraded to JDK 17 (17.0.2) and now it works...
I was watching a beginners Java tutorial by Kody Simpson on YT (youtube.com/watch?v=t9LP9Nt9Nco), and in that tutorial the boy Kody prints crazy symbols called Unicode like "☯Ωøᚙ", but for me it just prints "?" - a question mark.
char letter = '\u1699';
System.out.println(letter);
I tried pretty much every solution on Stack Overflow, such as:
- Changing File Encoding to UTF-8, although mine was using UTF-8 by default.
- Putting '-Dconsole.encoding=UTF-8' and '-Dfile.encoding=UTF-8' in the Edit Custom VM options.
- Messing with Region Settings in control panel.
None of it worked.
Every post was also from many years ago, such as this one, which is from 12 years:
unicode characters appear as question marks in IntelliJ IDEA console
I ended up deleting and re-downloading Intellij because I thought I messed up some settings and wanted a restart, but this time I made the Project SDK an older version, Oracle openJDK version 14.0.1, and now somehow it worked and printed the 'ᚙ' symbol.
Then I realized the problem might be the latest version of the JDK which is version 18, so I downloaded JDK 17.0.2, and it BOOM it still works and prints out the symbol 'ᚙ', so thats nice :). But when I switched back to JDK version 18 it just prints "?" again.
Also its strange because I can copy paste the ᚙ symbol into the writing code area whatever you call it, (on JDK version 18)
char letter = 'ᚙ';
System.out.println(letter);
But when I press RUN and try to PRINT ... it STILL GIVES QUESTION MARK.
I have no clue why this happens, I started learning coding 2 days so I'm probably dumb, or the new version has got a bug, but I never found a solution through Google or here, so this is why I'm making my first ever Stack Overflow post.
I can replicate your problem: printing works correctly when running your code if compiled with JDK 17, and fails when running your code if compiled with JDK 18.
One of the changes implemented in Java 18 was JEP 400: UTF-8 by Default. The summary for that JEP stated:
That sounds good, except one of the goals of that change was (with my emphasis added):
So I think your problem arose because you had ensured that the console's encoding in Intellij IDEA was UTF-8, but the
PrintStreamthat you were using to write to that console (i.e.System.out) was not.The Javadoc for
PrintStreamstates (with my emphasis added):Since your
PrintStreamwasSystem.out, you had not specified any "encoding or charset", and were therefore using the "default charset", which was presumably not UTF-8. So to get your code to work on Java 18, you just need to ensure that yourPrintStreamis encoding with UTF-8. Here's some sample code to show the problem and the solution:This is the output in the console when running that code:
Notes:
PrintStreamhas a new method in Java 18 namedcharset()which "returns the charset used in this PrintStream instance". The code above calls charset(), and shows that for my machine my "default charset" is windows-1252, not UTF-8.UPDATE: To address the issue raised in the comments below by Mostafa Zeinali, the
PrintStreamused bySystem.outcan be redirected to a UTF-8PrintStreamby callingSystem.setOut(). Here's sample code:This is the output from that code on my Windows 10 machine:
Note that
System.outis afinalvariable, so you can't directly assign a newPrintStreamto it. This code fails to compile with the error "Cannot assign a value to final variable 'out'":