aspose file tools*
The moose likes Beginning Java and the fly likes Why is Java cAse sEnSiTIvE? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why is Java cAse sEnSiTIvE?" Watch "Why is Java cAse sEnSiTIvE?" New topic
Author

Why is Java cAse sEnSiTIvE?

Tim Dee
Greenhorn

Joined: May 07, 2005
Posts: 22
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

Joined: Feb 08, 2004
Posts: 1281
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 ]

java amateur
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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!


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Tim Dee
Greenhorn

Joined: May 07, 2005
Posts: 22

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

Joined: Feb 08, 2004
Posts: 1281
this free e-book might interest you
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

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.


[Jess in Action][AskingGoodQuestions]
Tim Dee
Greenhorn

Joined: May 07, 2005
Posts: 22
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

Joined: Jul 08, 2003
Posts: 24166
    
  30

"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

Joined: Jul 11, 2001
Posts: 14112
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

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why is Java cAse sEnSiTIvE?
 
Similar Threads
Line number in servlet
case for java beans
Triangle Perimeter HELP!!!
Passing Database query results through a method to a JTable
java.lang.NullPointerException