The Java syntax

The first thing you'll need to understand is that Java is a programming language. A computer essentially does what you tell it to do - and a programming language like Java is a human-readable way to give a computer instructions.

That's a really, really simplified explanation, but it'll do for the purposes of this blog. I'm not trying to teach you all there is to know about the language (a full-year University course couldn't even do that!) but my aim is to teach you the fundamentals, teach you where to look for more information when you're not sure on a concept, and get you on your way with creating console applications. Eventually, we'll move onto creating a GUI application (but that's a long way down the road…)

So, back to giving a computer instructions.

A computer is dumb. Humans are smart. Will Smith is not half-robot.

A computer is dumb. I know, it makes all of those "robots taking over the world" movies look silly, but it's a fact. Now and in the foreseeable future, self-aware computers do not exist.

A computer is just a machine that does what you tell it to do. Through programming languages, you can make a computer appear intelligent and can make it do things a million times faster than a human could using pen and paper… but a computer is still dumb.

It doesn't understand if I plug in a microphone and say "What is two plus two?" - all that incoming sound is just data, and it's being manipulated using a program that a human programmer wrote.

So when you're talking to a computer, you need to get down to the computer's level, and speak in the language that the computer understands. That looks something like this (binary):

0101010100010111010100101010010101001010100101101010101110001111010100101101010101000101011011110101010100011010101110010101010100101010100010111010100101010010101001010100101101010101110001111010100101101010101000101011011110101010100011010101110010101010100101010100010101010101111010100101010010010101010010101010101000011110011010101001010010101010100101010101010000111011101010001111010010100101010101010100110010101101010010101010101000101010100000111100110101010010100101010101001010101010100001110111010100011110100101001010101010101001100101011010100101

The number 1 means to turn an electrical current on, and the number 0 means to turn an electrical current off. That's it. Believe it or not, some people write programs in binary!!!

In modern times, we have languages like Java that are pushed through a compiler (a bit like a human interpreter between Japanese and English) - and that compiler turns some human-understandable code into a bunch of 1's and 0's.

Again, that explanation is hugely simplified, and it's not just compilers that are involved in the process. All you need to know, is that some genius decided to write a program in binary and called it a "compiler", and today, we can use that compiler so that we don't need to write binary ourselves. Be very thankful! :)

Still, I haven't shown you what the Java syntax looks like. First, I'll give a really simple example.

Look at that code for a second, and see if you can understand what it means even if you don't have any programming experience whatsoever.

Probably me using the word 'answer' helped you quite a bit. One of the greatest things about modern programming languages like Java is that we can name a variable almost anything we want! (there are some exceptions, reserved words that can't used)

Normally you'd say something like 1 + 1 = 2, but in Java the answer is on the left hand side instead of on the right. This is called assignment. You have a variable named answer, and you are storing the answer (assigning the answer) to the mathematical problem to that variable.

So if I wanted to print out the value of answer later, the number 2 would appear on the computer screen.

Probably a simpler example of assignment follows:

Here we aren't doing any mathematics - we're just giving the variable answer a value of 6.

You can store words as well!

Variables don't need to store numbers - they can store a range of other things as well. Probably the other most common thing that you would store in a variable is a String. I used upper-case for the first letter for a reason, but I'll explain why in a future blog post.

A String is a collection is letters, numbers and other symbols. So for example, I could do this:

Now, compare that to the example I showed you before:

There are a couple of things to remember here:

  • When entering a String of words, the value needs to be surrounded in double-quotation marks " and ".
  • A variable can only be of one type. I used a different variable name (answerInWords) for the String value, because I've already used answer for numbers.

And semi-colons?

If you're perceptive, you will have noticed that there is a semi-colon ( ; ) after every code example that I've given so far.

In Java, the semi-colon tells the compiler that you have reached the end of a statement. A statement is usually a single instruction, and the semi-colon indicates the end of that instruction and the beginning of another.

Because of the semi-colon, this means that I can split things up on to multiple lines and it will still work:

That is exactly the same as this:

All white space (spaces and line breaks) are ignored by the compiler.

This will also work (two statements on one line):

But for code readability, it is generally accepted that you should place all statements on their own lines, and not split them into multiple lines. That helps someone that is reading your code to quickly understand what you're trying to do, because it is easier to read when each statement is on it's own line.

So the previous code block should have been written like this:

camelCase

Camel Case is named because there are humps only in the middle, just like a camel.

  • The first word always begins with a lower-case letter
  • All other words begin with capital letters
  • There are no spaces and no special characters

Camel case is the recommended way to name your variables in Java, and pretty much any other programming language. There is one exception (constants) but that's beyond the scope of this blog post.

Just like the one-statement-per-line rule, you should also use camel case for variable names, method (function) names, and pretty much everything else that you give a name to.

I want to start programming!

Patience, young Jedi. Even if you've programmed in other languages before, and this seems like child's play to you, it's still important that you fully grasp the basics. Because everything depends on you knowing how this works.

Terminology

Now, I've been a bit relaxed on terminology so far, so I'll explain the proper terms now and will begin using the proper terms in future blog posts:

Data Type
The type of value that a variable is allowed to store. For example, variableInWords has a data type of String.
String
A collection of letters, numbers and symbols (including spaces) that can be stored in a variable. It's a data type.
int
A data type for numbers, or integers. It can store positive or negative whole numbers (integers). Depending on the computer architecture (e.g. 32-bit or 64-bit) an integer has a limit to how big the number that it stores can be. Programs written for a specific architecture might have compatibility issues running on a different architecture.
double
A data type for numbers, which accepts any positive or negative numbers even if they are not whole numbers. An example of a double is 4.5
long
A data type to hold bigger numbers, that the int data type normally can't hold.

You should also make sure you understand the meaning of any words I've put in bold throughout this blog post. Feel free to ask in the comments if you're not sure, or want to clarify anything. I'll be more than happy to help you to understand.




Get some feedback on what you've learnt so far

Want to test your understanding of what you've learnt so far? Copy and paste this into your comment, replacing anywhere it has **** with your answer. It doesn't matter if you get it wrong - because I'll explain it again for you so you can get it right next time.

I'll reply to everyone that does this, even if you get it all correct.

[[collapsible show="Show my answers" hide="Hide my answers"]]

Q1. Write a Java statement that adds the numbers 2 and 3 together and stores it in the variable named {{total}}.

[[code type="java"]]
****
[[/code]]

Q2. Make up a descriptive variable name in camel case for these variables:

* The manufacturer of a car -- ****
* Someone's first and last name both stored in the same variable -- ****
* The botanical name for a plant or animal -- ****

Q3. In Java, what is 'assignment' ?

****

[[/collapsible]]

Comments (11)

Timothy Foster

Re: The Java syntax
11 Jun 2010 23:22. Edited 0 times. (Edit, Quote[[div class="quote"]]
**[*http://java.wikidot.com/comment:5 Timothy Foster wrote]:**
[[include comment:5]]
[[/div]]
, PermalinkRight-click > Copy link address:
http://java.wikidot.com/comment:5
)

By the way, I have a question on assigning variables values. In Javascript and another programming language I know, you have to declare that you are defining a variable as such:

var answer = 1+1;

That other language:

void main(){
    int nAnswer = 1+1;
    string sAnswer = "two";
}

Is this not required in Java?

Included page "inc:signature" does not exist (create it now)

leiger

Re: The Java syntax
11 Jun 2010 23:39. Edited 1 times. (Edit, Quote[[div class="quote"]]
**[*http://java.wikidot.com/comment:7 leiger wrote]:**
[[include comment:7]]
[[/div]]
, PermalinkRight-click > Copy link address:
http://java.wikidot.com/comment:7
)


To everyone else: Don't cheat! ;-)

Yes, it's required to give a variable a data type first. As I said each variable can only be one type, and you need to declare that first.

You don't need to declare it at the same time as you give it a value - and you can change a variable's value later. That means that the examples I gave in the blog post will work if they're added to a program that already declares that variable somewhere else.

I'm going to explain this in my next blog post, along with a few other related concepts… :)

Included page "inc:signature" does not exist (create it now)

tsangk

Re: The Java syntax
15 Jun 2010 08:02. Edited 0 times. (Edit, Quote[[div class="quote"]]
**[*http://java.wikidot.com/comment:8 tsangk wrote]:**
[[include comment:8]]
[[/div]]
, PermalinkRight-click > Copy link address:
http://java.wikidot.com/comment:8
)

Included page "inc:signature" does not exist (create it now)

leiger

Re: The Java syntax
16 Jun 2010 01:22. Edited 0 times. (Edit, Quote[[div class="quote"]]
**[*http://java.wikidot.com/comment:9 leiger wrote]:**
[[include comment:9]]
[[/div]]
, PermalinkRight-click > Copy link address:
http://java.wikidot.com/comment:9
)

You read my post first didn't you? :P

Included page "inc:signature" does not exist (create it now)

tsangk

Re: The Java syntax
16 Jun 2010 06:47. Edited 0 times. (Edit, Quote[[div class="quote"]]
**[*http://java.wikidot.com/comment:10 tsangk wrote]:**
[[include comment:10]]
[[/div]]
, PermalinkRight-click > Copy link address:
http://java.wikidot.com/comment:10
)

Actually…. no - I didn't look at the answers until afterwards :P

Included page "inc:signature" does not exist (create it now)

leiger

Re: The Java syntax
16 Jun 2010 07:22. Edited 0 times. (Edit, Quote[[div class="quote"]]
**[*http://java.wikidot.com/comment:11 leiger wrote]:**
[[include comment:11]]
[[/div]]
, PermalinkRight-click > Copy link address:
http://java.wikidot.com/comment:11
)

Well, tick tick tick, all correct. :) Just check my reply to Timothy for some comments about it… feel free to add your own comments if you want as I'm not infallible myself and could make a mistake or forget to mention something :)

Included page "inc:signature" does not exist (create it now)

soronlin

Re: The Java syntax
22 Jun 2010 11:06. Edited 2 times. (Edit, Quote[[div class="quote"]]
**[*http://java.wikidot.com/comment:12 soronlin wrote]:**
[[include comment:12]]
[[/div]]
, PermalinkRight-click > Copy link address:
http://java.wikidot.com/comment:12
)

Edit: I didn't cheat either, honest. Great minds think alike. ;-)

By the way, I have an essay on "What is Software?" on my Website. The Open-Source book on the left sidebar is also an excellent introduction to programming, albeit in Python, not Java, but for the first chapter (which is the important bit,) that doesn't matter.

Included page "inc:signature" does not exist (create it now)

leiger

Re: The Java syntax
22 Jun 2010 23:22. Edited 0 times. (Edit, Quote[[div class="quote"]]
**[*http://java.wikidot.com/comment:13 leiger wrote]:**
[[include comment:13]]
[[/div]]
, PermalinkRight-click > Copy link address:
http://java.wikidot.com/comment:13
)

Will have to check that link later, gotta go.

- Shane

Included page "inc:signature" does not exist (create it now)

rhombus p

Re: The Java syntax
17 May 2011 18:19. Edited 0 times. (Edit, Quote[[div class="quote"]]
**[*http://java.wikidot.com/comment:14 rhombus p wrote]:**
[[include comment:14]]
[[/div]]
, PermalinkRight-click > Copy link address:
http://java.wikidot.com/comment:14
)

A couple things to note

"String" in java actually starts with a capitol "S" and is not a primative data type (dont worry about this yet)

Vars in java can contain specials chars (for example, if your used to PHP you can put a $varname, its just not the Convention)

Included page "inc:signature" does not exist (create it now)

Lladnarius 2400

Re: The Java syntax
11 Jan 2021 20:42. Edited 0 times. (Edit, Quote[[div class="quote"]]
**[*http://java.wikidot.com/comment:15 Lladnarius 2400 wrote]:**
[[include comment:15]]
[[/div]]
, PermalinkRight-click > Copy link address:
http://java.wikidot.com/comment:15
)

(sorry if this annoys you and if youve forgotten about this site)

I have no experience with coding, and i have tried learning basic javascript on youtube videos and from other blogs; yours is 100% the best

Included page "inc:signature" does not exist (create it now)

I m your dad poop

16
01 Jan 2023 13:53. Edited 1 times. (Edit, Quote[[div class="quote"]]
**[*http://java.wikidot.com/comment:16 I m your dad poop wrote]:**
[[include comment:16]]
[[/div]]
, PermalinkRight-click > Copy link address:
http://java.wikidot.com/comment:16
)


Is……Is this site abandoned?

Included page "inc:signature" does not exist (create it now)

Post Comment Add reply on "The Java syntax"
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License