• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why is Java cAse sEnSiTIvE?

 
Tim Dee
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this a real dumb question? Is there a real good reason for having Java code case sensetive?

I'm muddling my way through the book 'Java Head First' and sorta getting there. Slowly. Remembering the commands is hard enough but also remembering the upper/lower info is maddening and seems unnecessary.

Most are lower case such as int, while, public etc then every now and again there is a capital; String, System etc. WHY???

There has to be a good reason for it, it can't just be for aesthetics. Can it?
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most are lower case such as int, while, public etc then every now and again there is a capital; String, System etc. WHY???
if you take a closer look you'll see that those starting with capital allways refer to class names, while all the others are method names, variable names and so on.

there's a set of rules and conventions you'll gradually absorb

one example
when you declare a variable being of type "String", you write:
notice that i used camel notation in naming my variable so that's easier to read
[ May 22, 2005: Message edited by: miguel lisboa ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This convention also allows you to write something like

Customer customer;

To someone used to the Sun coding convention, it's immediately obvious that Customer is a class/interface, whereas customer is a variable of type Customer.

You will get used to it, too!
 
Tim Dee
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

notice that i used camel notation in naming my variable so that's easier to read

[ May 22, 2005: Message edited by: miguel lisboa ][/qb]<hr></blockquote>

By Camel notation you mean a capital at the new word yes? So the name has a hump in it I guess.

So it's illegal to start a variable or string with a capital, EVER? I guess that makes them easier to spot especialy with the 'Camel Notation' system.

Many thanks for the help.

[ May 22, 2005: Message edited by: Tim Dee ]
[ May 22, 2005: Message edited by: Tim Dee ]
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this free e-book might interest you
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tim Dee:

So it's illegal to start a variable or string with a capital, EVER?


No, it's just a convention -- a universally obeyed one, but just a convention. You can break the convention, but then your code is harder for seasoned Java programmers to understand.

Given that something already has a name, though, you have to use the capitalization that was used to define it. Hence, the class String always will start with a capital "S", because that's how it was defined.
 
Tim Dee
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK that makes sense. Thanks.

So when you see something like:
System.out.println("Hello");

System is a Class, and therefore a capital. println("Hello") is a method and lowercase.

But where does the 'out' come into it? It's not a class. Is 'println()' something that has been inherited from 'out'?

Thanks again.
Tim
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"out" is a public, static member variable in the System class (more properly, the java.lang.System class.) "out" is an instance of the java.io.PrintStream class, which has a number of overloaded "println" methods.

So you're saying "call the "println" method on the "out" member of the "System" class."

You'll learn all this stuff in due time. I know it sounds crazy now but you know, that's about as complicated as it gets, and once that all makes sense, you're set.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's actually the beauty of the convention - consider those three different lines:

System.out.println("Hello");

If this line conforms to the Sun coding convention, out has to be a variable - in this context it actually has to be a static field.

System.out().println("Hello");

If this were correct code, it would mean that out is a method on the System class (static again).

System.Out.println("Hello");

A seasoned java programmer will conclude from this line that Out is a nested class of the System class.

System.Out().println("Hello");

This is a syntax error. According to the convention, Out() can only refer to a constructor, so there is missing a "new" in the line.

Do you see how the convention makes it much easier to interprete Java code?
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tim Dee:
Is this a real dumb question? Is there a real good reason for having Java code case sensetive?

I'm muddling my way through the book 'Java Head First' and sorta getting there. Slowly. Remembering the commands is hard enough but also remembering the upper/lower info is maddening and seems unnecessary.

Most are lower case such as int, while, public etc then every now and again there is a capital; String, System etc. WHY???

There has to be a good reason for it, it can't just be for aesthetics. Can it?


To answer your original question, Java is case-sensitive because computers are inherently case-sensitive. First you should understand how the computer stores characters. A character is any symbol you can type on the keyboard (plus some other symbols that you can't, but we won't go there). The computer associates each of these symbols with a number. In the U.S., many older computers use what is called the ASCII table for this association. In more modern computers, and particularly in Java, the Unicode standard is used to determine which number is associated with each character. In this scheme, upper- and lower-case letters each have a unique number. For example, 'A' is number 65 and 'a' is number 97. Typically in programming, these exact values are unimportant as you can use character literals (such as 'A') to indicate a character.

This means that in order to make software so it ISN'T case-sensitive, the programmers have to go to extra work in order to make sure that 'a' and 'A' are the same thing. In this case, the creators of Java decided not to go to that extra effort. I would guess that one reason is because Java is very similar to C++ which is also case-sensitive.

Layne
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tim Dee:

notice that i used camel notation in naming my variable so that's easier to read

[ May 22, 2005: Message edited by: miguel lisboa ]<hr></blockquote>

By Camel notation you mean a capital at the new word yes? So the name has a hump in it I guess.

So it's illegal to start a variable or string with a capital, EVER? I guess that makes them easier to spot especialy with the 'Camel Notation' system.

Many thanks for the help.

[ May 22, 2005: Message edited by: Tim Dee ]

[ May 22, 2005: Message edited by: Tim Dee ][/QB]


You are on the right track. CamelCase (or CamelNotation) means that each new word starts with a capital letter. This is a common convention used in Java. However, as noted above, it is just a convention. The compiler will let you use wHateVer CapItAliZaTion you wish. However, following conventions makes code much easier to read.

Layne
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic