Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Java syntax
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Basics== ===Identifier=== An [[Identifier#In computer languages|identifier]] is the name of an element in the [[source code|code]]. There are certain standard [[Naming conventions (programming)|naming conventions]] to follow when selecting names for elements. Identifiers in Java are [[Case sensitivity|case-sensitive]]. An identifier can contain: * Any Unicode character that is a letter (including numeric letters like [[Roman numerals]]) or digit. * [[Currency sign]] (such as Β₯). * Connecting punctuation character (such as [[Underscore|_]]). An identifier cannot: * Start with a digit. * Be equal to a reserved keyword, null literal or [[Boolean data type|Boolean]] literal. ===Keywords=== {{Main|List of Java keywords}} ====Keywords==== The following words are keywords and cannot be used as identifiers under any circumstances. {{div col|colwidth=15em}} * <code>_</code> * <code>abstract</code> * <code>assert</code> * <code>boolean</code> * <code>break</code> * <code>byte</code> * <code>case</code> * <code>catch</code> * <code>char</code> * <code>class</code> * <code>continue</code> * <code>default</code> * <code>do</code> * <code>double</code> * <code>else</code> * <code>enum</code> * <code>extends</code> * <code>final</code> * <code>finally</code> * <code>float</code> * <code>for</code> * <code>if</code> * <code>implements</code> * <code>import</code> * <code>instanceof</code> * <code>int</code> * <code>interface</code> * <code>long</code> * <code>native</code> * <code>new</code> * <code>package</code> * <code>private</code> * <code>protected</code> * <code>public</code> * <code>return</code> * <code>short</code> * <code>static</code> * <code>super</code> * <code>switch</code> * <code>synchronized</code> * <code>this</code> * <code>throw</code> * <code>throws</code> * <code>transient</code> * <code>try</code> * <code>void</code> * <code>volatile</code> * <code>while</code> {{div col end}} ====Reserved identifiers==== The following words are contextual keywords and are only restricted in certain contexts. {{div col|colwidth=15em}} * <code>exports</code> * <code>module</code> * <code>non-sealed</code> * <code>open</code> * <code>opens</code> * <code>permits</code> * <code>provides</code> * <code>record</code> * <code>requires</code> * <code>sealed</code> * <code>to</code> * <code>transitive</code> * <code>var</code> * <code>when</code> * <code>with</code> * <code>yield</code> {{div col end}} ====Reserved words for literal values==== The following words refer to literal values used by the language. {{div col|colwidth=15em}} * <code>true</code> * <code>false</code> * <code>null</code> {{div col end}} ====Unused==== The following words are reserved as keywords, but currently have no use or purpose. {{div col|colwidth=15em}} * <code>const</code> * <code>goto</code> * <code>strictfp</code> {{div col end}} ===Literals=== {| class="wikitable" |- !colspan="2"|Integers |- ![[Binary numeral system|binary]] (introduced in Java SE 7) |{{mono|0b11110101}} ({{mono|0b}} followed by a binary number) |- ![[octal]] |{{mono|0365}} ({{mono|0}} followed by an octal number) |- ![[hexadecimal]] |{{mono|0xF5}} ({{mono|0x}} followed by a hexadecimal number) |- ![[decimal]] |{{mono|245}} (decimal number) |- !colspan="2"|[[Floating-point]] values |- !rowspan="2" | float |{{mono|23.5F}}, {{mono|.5f}}, {{mono|1.72E3F}} (decimal fraction with an optional exponent indicator, followed by {{mono|F}}) |- |{{mono|0x.5FP0F}}, {{mono|0x.5P-6f}} ({{mono|0x}} followed by a hexadecimal fraction with a mandatory exponent indicator and a suffix {{mono|F}}) |- !rowspan="2" | double |{{mono|23.5D}}, {{mono|.5}}, {{mono|5.}}, {{mono|1.72E3D}} (decimal fraction with an optional exponent indicator, followed by optional {{mono|D}}) |- |{{mono|0x.5FP0}}, {{mono|0x.5P-6D}} ({{mono|0x}} followed by a hexadecimal fraction with a mandatory exponent indicator and an optional suffix {{mono|D}}) |- !colspan="2"|Character literals |- !char |{{mono|'a'}}, {{mono|'Z'}}, {{mono|'\u0231'}} (character or a character escape, enclosed in single quotes) |- !colspan="2"|Boolean literals |- !boolean |{{mono|true}}, {{mono|false}} |- !colspan="2"|null literal |- !null reference |{{mono|null}} |- !colspan="2"|String literals |- !String |{{mono|"Hello, World"}} (sequence of characters and character escapes enclosed in double quotes) |- !colspan="2"|Characters escapes in strings |- ![[Unicode]] character |{{mono|\u3876}} ({{mono|\u}} followed by the hexadecimal unicode code point up to U+FFFF) |- ![[Octal]] escape |{{mono|\352}} (octal number not exceeding 377, preceded by backslash) |- ![[Line feed]] |{{mono|\n}} |- ![[Carriage return]] |{{mono|\r}} |- ![[Form feed]] |{{mono|\f}} |- ![[Backslash]] |{{mono|\\}} |- ![[Single quote]] |{{mono|\'}} |- ![[Double quote]] |{{mono|\"}} |- ![[Tab character|Tab]] |{{mono|\t}} |- ![[Backspace]] |{{mono|\b}} |} Integer literals are of <code>int</code> type by default unless <code>long</code> type is specified by appending <code>L</code> or <code>l</code> suffix to the literal, e.g. <code>367L</code>. Since Java SE 7, it is possible to include underscores between the digits of a number to increase readability; for example, a number {{mono|145608987}} can be written as {{mono|145_608_987}}. ===Variables=== [[Variable (computer science)|Variables]] are identifiers associated with values. They are declared by writing the variable's type and name, and are optionally initialized in the same statement by assigning a value. <syntaxhighlight lang="java"> int count; //Declaring an uninitialized variable called 'count', of type 'int' count = 35; //Initializing the variable int count = 35; //Declaring and initializing the variable at the same time </syntaxhighlight> Multiple variables of the same type can be declared and initialized in one statement using comma as a delimiter. <syntaxhighlight lang="java"> int a, b; //Declaring multiple variables of the same type int a = 2, b = 3; //Declaring and initializing multiple variables of the same type </syntaxhighlight> ====Type inference==== {{Main|Type inference}} Since Java 10, it has become possible to [[type inference|infer types]] for the variables automatically by using <code>var</code>. <syntaxhighlight lang="java"> // stream will have the FileOutputStream type as inferred from its initializer var stream = new FileOutputStream("file.txt"); // An equivalent declaration with an explicit type FileOutputStream stream = new FileOutputStream("file.txt"); </syntaxhighlight> ===Code blocks=== The separators {{mono|{{(}}}} and {{mono|{{)}}}} signify a code block and a new scope. Class members and the body of a [[Method (computer programming)|method]] are examples of what can live inside these braces in various contexts. Inside of method bodies, braces may be used to create new scopes, as follows: <syntaxhighlight lang="java"> void doSomething() { int a; { int b; a = 1; } a = 2; b = 3; // Illegal because the variable b is declared in an inner scope.. } </syntaxhighlight> ===Comments=== Java has three kinds of [[Comment (computer programming)|comments]]: ''traditional comments'', ''end-of-line comments'' and ''documentation comments''. Traditional comments, also known as block comments, start with <code>/*</code> and end with <code>*/</code>, they may span across multiple lines. This type of comment was derived from C and C++. <syntaxhighlight lang="java"> /* This is a multi-line comment. It may occupy more than one line. */ </syntaxhighlight> End-of-line comments start with <code>//</code> and extend to the end of the current line. This comment type is also present in C++ and in modern C. <syntaxhighlight lang="java"> // This is an end-of-line comment </syntaxhighlight> Documentation comments in the source files are processed by the [[Javadoc]] tool to generate documentation. This type of comment is identical to traditional comments, except it starts with <code>/**</code> and follows conventions defined by the Javadoc tool. Technically, these comments are a special kind of traditional comment and they are not specifically defined in the language specification. <syntaxhighlight lang="java"> /** * This is a documentation comment. * * @author John Doe */ </syntaxhighlight> ===Universal types=== Classes in the package java.lang are implicitly imported into every program, as long as no explicitly-imported types have the same names. Important ones include: ====java.lang.Object==== {{code|java.lang.Object}} is Java's [[top type]]. It is implicitly the superclass of all classes that do not declare any parent class (thus all classes in Java inherent from <code>Object</code>. All values can be converted to this type, although for primitive values this involves [[Object type (object-oriented programming)#Autoboxing|autoboxing]]. ====java.lang.String==== {{code|java.lang.String}} is Java's basic string type. [[immutable object|Immutable]]. Some methods treat each [[UTF-16]] code unit as a "character", but methods to convert to an <code>int[]</code> that is effectively [[UTF-32]] are also available. ====java.lang.Throwable==== {{code|java.lang.Throwable}} is supertype of everything that can be [[exception handling|thrown or caught]] with Java's <code>throw</code> and <code>catch</code> statements.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)