Home arrow Java arrow Page 2 - Java Part 2: Syntax
JAVA

Java Part 2: Syntax


In this, part two of this ten part series on Java, Chris looks at the Java language syntax, variable declerations, truth conditions, loops and more. He finishes by wrapping everything up into a simple Java function.

Author Info:
By: Chris Noack
Rating: 5 stars5 stars5 stars5 stars5 stars / 9
November 27, 2001
TABLE OF CONTENTS:
  1. · Java Part 2: Syntax
  2. · Decleration and assignment
  3. · Making code more readable
  4. · Truth operations
  5. · Iterations and looping
  6. · The while loop
  7. · Putting it all together
  8. · Conclusion

print this article
SEARCH DEVARTICLES

Java Part 2: Syntax - Decleration and assignment
(Page 2 of 8 )

Java variables are declared in exactly the same way as in both C and C++. Let's declare a variable of type int and then set it to a value:

int val;

val = 1;


This decleration could be shorted to just one line of code, like this:

int val = 1;

Addition and Subtraction

The standard C incremetor function ++, or -- for decrementing a variable still holds in Java. For example:

int val = 1;

val = val + 1; // val is now 2


Further, we can simplify the second line even more:

val++;

This works the same for decrementing. These two operators for incrementing and decrementing are known as unary operators because they only require one argument.

Multiplication and Division

To multiply a variable by a numerical value, we use the * operator, like this:

val = val * 10; // Multiply val by 10

We can also multiply a variable by a number like this:

val *= 10; // Same as val = val * 10

Multiplying one variable by another is also simple:

var = var * par;

OR

var *= par;

Division follows exactly the same principles as addition, subtraction and multiply:

val = val / 50;

OR

val /= 50;

Also, dividing one value by another is easily accomplished:

val = val / par; // Same as val = val / par

OR

val /= par; // Same as val = val / par
blog comments powered by Disqus
JAVA ARTICLES

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials