简体   繁体   中英

Do IDEs and compilers have different spacing for /t spacing? (JAVA on IntelliJ)

I am pretty new to programming and just started my computer science degree. In class I was given an assignment to create a pattern. They use jGRASP as their IDE in class while I use IntelliJ.

I created a pattern using the IntelliJ IDE to display "AEV" as displayed in the screen shot Code and output ScreenShot . I used blank spaces and /t to create the pattern. To check if my code looks the same as they want I took the .java file and ran it in another IDE called jGRASP. Now on jGRASP the spacing is all off jGRASP code and output ScreenShot . I couldn't figure out what caused it.

Do "/t" have different spacing for different IDEs? Or is there some setting that I need to change?

Very likely. The standard tab width is 8, but some people prefer to display 4-column tabs, or even 2-column tabs. There's also people who prefer to indent with tabs, and people that prefer to indent with spaces. This is the stuff flame wars are fought over. Whichever camp you're in, the worst possible choice is to mix the two in the same file. As long as all indents are tabs, it doesn't matter how tabs are displayed, it will always work okay. If there are no tabs and all indents are spaces, it matters even less what the tab width is. But if you make a hybrid file, expect trouble.

Similarly for the output: in general, I'd only use Tab semantically, like in TSV to separate records, never for precise formatting, precisely because you have no control over how it will be viewed.

Do IDEs and compilers have different spacing for /t spacing? (JAVA on IntelliJ)

The compiler doesn't care.

IDE's typically typically have a default TAB width that you can change. The default may depend on the host operating system.

The real problem is that operating systems have different defaults ... that you typically can't change.

Problems arise if you write your source code on (say) Windows and your co-worker uses Linux or Mac. If you use TAB and you don't agree to configure your IDEs to have the same number of spaces for TAB, your code will look ugly to your co-worker, and vice versa.

The best solution is to not use the TAB character for indentation. Configure your IDE to use spaces for indentation. Then your code will be correctly indented irrespective of the IDE or OS anyone uses to view it. Note that modern IDEs can easily be configured to translate a TAB keystroke into the appropriate number of spaces for an indentation level.


However I think my question was a bit unclear. I was wondering why the "\\t" in a print() would have different output on run I/O window.

That is entirely down to how the console displays the TAB character in the output stream. The rendering of TAB to some number of fixed width spaces is typically done by some OS specific / OS provided software. It is outside of Java's control. Indeed, the JVM typically find out how (or even if) this rendering will occur, so it cannot reasonably intervene.

When console output is displayed by an IDE, the IDE takes care of the rendering.

Would you know why the spacing in the output is different for IntelliJ vs jGRASP?

That would be a design decision by the people who implemented the respective tools. But I imagine you can configure those tools to behave differently to their respective default settings. Check the configuration settings ...

But the bottom line is that if you want console text spacing to be consistent across all real / virtual consoles, output SP characters rather than TABs.

Finally found a solution. The problem was indeed with the tab spacing for the two IDEs. jGrasp by default uses 3 spacing tabs while IntelliJ for Java uses 4 spacing.

On jGRASP if you click on settings > Font > CSD > Tab size : uncheck button to edit. jGrasp setup

On IntelliJ it is a bit harder to change the tab since it creates a project when you first create it.

Go to preferences > Editor > CodeStyle : uncheck editor config support. If this is kept checked then the editorconfig file in the package overrides the IDE settings. IntelliJ Code style setup

CodeStyle dropdown> select file (Java) > Tabs and Indents > set Tab to 3 IntelliJ Java File setup Click apply.

Finally go to CodeStyle again and click export on editorconfig to replace the file on the project.

The EditorConfig file and the editorconfig support option is what causes Intellij to be more confusing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM