Is Java Case Sensitive?

Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

Is Java Case Sensitive? Most programming languages expect an exact value for any operation. A y is not a Y! This lesson will highlight Java's case sensitivity and provide code examples to show how objects are named.

Case is Important

Yes, Java is case sensitive. This might seem a little frustrating, especially when you realize why your program isn't running right. The variable you sent (TreeCount) is really named treeCount! There is a reason for this, and it's really at the level of bits and bytes. To a computer, t is not T. This can frustrate a human, but the computer doesn't care. They are completely different values in memory. Therefore, the following code is perfectly acceptable (although it might drive your quality analyst or tester crazy).

// These are completely different variables!
int treeCount = 15;
int TreeCount = 25;

It is recommended that you name variables consistently. It is standard practice to use lowercase for variable names, only using uppercase letters within the word for readability such as treeCount, pageNum, or recordCount. Classes are named with uppercase letters such as Tree, Vehicle, or CardGame. Let's take a look at some examples of how case plays a role in Java.

Examples of Case in Java

In order to reinforce the concept, let's take a look at some examples where the case sensitivity serves a purpose. Let's start with classes and their variables.

Classes and Variables

Recall that classes are named with uppercase letters. For example, we can have a Tree class:

public class Tree {
  //Tree class
  public void showMsg() {
   System.out.println("Tree Here");
  }
}

We can create in instance of this class. At first, the following code may be confusing. But remember that variables are typically lowercase, classes are uppercase. Therefore, using the same name for BOTH class and instance variable actually helps us differentiate.

public static void main(String[] args) {
  Tree tree = new Tree();
  tree.showMsg();
}

Methods

Like variables, methods and interfaces are lowercase. In our Tree class, we actually declared a method called showMsg(). This is a method that prints out a display. Other methods in this class would also be named in lowercase. For example, the following method takes in a variable and does some math.

public double calcPhoto(double ps) {
  double photoS = ps;
  photoS *= .000354;
  long leaves = 1349934234;
  double total = photoS * leaves;
  return total;
}

Constructors and Interfaces

Constructors and interfaces are special Java tools. We don't need to worry about how they work right now, but we need be aware of how they are named. An interface is like a Java class, and a constructor is a set of instructions that run each time a new instance of a class is created. Because they are class-focused, their names are uppercase.

Take a look at the following code that declares a Tree interface. Don't worry about how this would work, but pay attention to how the interface AND the methods are named.

interface Tree {
  public void grow();
  public void photoSynth();

A constructor, or constructors, can be created also. If you use them, they are implemented each time a new instance of a class is created. Again, we don't need to focus on the details of how they work, but on how they are named.

The following constructors were created for the Tree class:

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support