-5

I want to understand the use of semicolons in Java expressions, statements, or in for loops. and I want to know what is the memory consumption for the semicolon and comments we write in the source code?.

int a = 1; //coments written in java
for(int a=0; a <1;a++);

etc

If I remove the semicolon gives below errors

java: ';' expected

Naveen R
  • 1
  • 2
  • 3
    A semicolon is used to end an [expression statement](https://docs.oracle.com/javase/specs/jls/se17/html/jls-14.html#jls-14.8). The language says you need one, so why are you surprised when you get a syntax error by omitting it? About memory usage, what do you mean? – Federico klez Culloca Dec 27 '22 at 13:52
  • every bit in java requires some memory to store, i want to know if semicolon requires any memory space in stack ? – Naveen R Dec 27 '22 at 13:56
  • 2
    "every bit in java requires some memory to store" no, it doesn't, unless you mean the space taken on disk by the source code. – Federico klez Culloca Dec 27 '22 at 13:56
  • Small correction about my first comment: there are other kind of statements other than just expression statements that end with a semicolon, for example empty statements and `do {} while(condition);`. – Federico klez Culloca Dec 27 '22 at 13:58
  • so, you mean comments, semicolons..etc would be ignored by JVM during processing/executing the source code? – Naveen R Dec 27 '22 at 13:59
  • >> so, you mean comments, semicolons..etc would be ignored by JVM during processing/executing the source code? Exactly. Please read those answers below ;) – Sold Out Dec 27 '22 at 14:01
  • @NaveenR Sure. The generated code (java binary code here) does not include any comments or other syntactical stuff from the source. The binary code (of whatever language) is _very_ different from the source code. – PMF Dec 27 '22 at 14:01
  • 4
    The JVM doesn't execute the source code. It executes bytecode. The problem with your question is that you need to clarify what you mean by "memory consumption". You mean the memory used by the compiler? By the JVM? The storage space used to hold the source code? The storage space needed to hold the compiled bytecode? – Federico klez Culloca Dec 27 '22 at 14:01
  • @SoldOut Java is a bit particular in that sense. Empty lines or lines that are only made of comment *do* make a difference in the resulting bytecode. See [here](https://stackoverflow.com/questions/52625161/why-does-a-java-class-compile-differently-with-a-blank-line/52625304#52625304) – Federico klez Culloca Dec 27 '22 at 14:03
  • now I got how that works, thanks @sold out, my question needs to be edited in that case – Naveen R Dec 27 '22 at 14:08
  • @FedericoklezCulloca Thanks for the note. Sure - in debug mode this is true for all C derivatives. Code lines do link to the real bytecode - for debugging needs, so the line numbers (even empty and just commented lines) do matter in t his P.O.V. But I think that is beyond the scope of this Q, right ? – Sold Out Dec 27 '22 at 14:31
  • @SoldOut my broader point was that I wouldn't assume that things that are semantically irrelevant don't change the compiler output. But yeah, it doesn't apply to *this* question in particular. Case in point: make a simple hello world, add empty statements and comments at the end of the line (beware: not on new lines!) that prints the string: compiler output is exactly the same. – Federico klez Culloca Dec 27 '22 at 14:38

2 Answers2

3

The semicolon is the statement delimiter. It's not part of the instructions, it separates them. It's just a hint to the compiler, it doesn't generate any code in itself.

Generally speaking, there's no relation between the length of the source code and the size of the binary file, particularly because the length of the visible source code is typically much smaller than that of all the libraries used.

PMF
  • 14,535
  • 3
  • 23
  • 49
0

The reason for semicolon in all c-family languages is, that the code parser (part of compiler) must know somehow, when is the expression complete. Programmer is free to type another statement(s) in that same line, each delimited by a semicolon. In some other languages, end of line character denotes end of expression. As to the memory consumption, with ASCII coding (or variable length coding), it would be 8 bits = 1 byte each colon. But careful, this will only consume memory while you edit the code in editor. When you compile/execute your code - those semantic characters like colons, commas, brackets, etc are not held in memory anymore. As stated before, this is only needed for compiler (and editor of course). Does this explain ?

Sold Out
  • 1,321
  • 14
  • 34
  • Yes, makes sense now, thanks for explaining it. like you said all those semantic characters are stored in editor memory right? how about comments written in the source code? comments any way cant be ignored during compilation right? – Naveen R Dec 27 '22 at 14:05
  • @NaveenR Certainly comments are the same case as "semantic sugar" characters. A modern compiler will optimize for you EVEN most of your code behind scenes - and certainly it will not put anything unnecessary (like comments) to your binary (executable) - assembly files. I think that should be obvious by now.. – Sold Out Dec 27 '22 at 14:19