Copyright

Java Statements: Definition & Examples

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: Java's 'Hello World': Print Statement & Example

You're on a roll. Keep up the good work!

Take Quiz Watch Next Lesson
 Replay
Your next lesson will play in 10 seconds
  • 0:04 Java Statements
  • 0:34 Java Methods
  • 1:35 Java Exceptions
  • 3:30 Lesson Summary
Save Save Save

Want to watch this again later?

Log in or sign up to add this lesson to a Custom Course.

Log in or Sign up

Timeline
Autoplay
Autoplay
Speed Speed

Recommended Lessons and Courses for You

Lesson Transcript
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.

In this lesson, we'll learn the definition of a Java statement and discuss some of the different types. We'll also explore some working code examples for this important programming concept.

Java Statements

Java statements are instructions that tell the programming language what to do. A basic statement, like an assignment statement, assigns a value to a variable, as shown in this image:

double entryFee = 15.75;

All Java statements must end in a semicolon (;). This tells Java that it should attempt to process all information up to that semicolon.

In our example, we declared a variable and gave it a value. For the next couple of examples, we'll look at some Java methods that offer a different way to declare a variable.

Java Methods

Java methods are blocks of code that perform a task, and can be used by other parts of a computer program, such as a declaration statement. For example, one basic function of a computer is display information to the screen. Why should we have to re-write that logic over and over? Instead, this Java declaration statement uses a delivered function to display information on multiple lines.

System.out.println("Line 1");
System.out.println("Line 2");

And the output of the previous code:


Java statement print output


Remember, each Java statement ends with a semicolon.

We've just seen the basic variable declaration statement in action. Now let's look at another example you'll see a lot: a Java string statement, or a collection of characters or objects. For the purposes of this lesson, we won't be delving into the nitty gritty of Java classes or blueprints and objects. We'll just focus on the statements themselves and how they're formulated.

String entryCode = new String();

Now that we've covered some basic Java statements, let's take a look at some others that you're likely to see. There are always some exceptions to the rules, like the semicolon rule.

Java Exceptions

The exception to the semicolon rule isn't exactly an exception, but a different approach to starting and ending statements. Some core statements in Java don't need to end in a semicolon. Instead, they're defined using curly brackets or braces. However, the statements between these brackets must end in semicolons.

First, we'll start with the basic if statement, common in any programming language. It checks to see if something is true. An if statement begins with an opening bracket, includes some statements, and ends with a closing bracket. Let's look at an example:

if(ticker > 15) {
  System.out.println("Over the limit!");
}

The main statement is enclosed within the brackets, and the statement within the block ends with a semicolon. Here we've added a statement that denotes the display or print method.

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