<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sjue Bsjs</title>
    <description>The latest articles on DEV Community by Sjue Bsjs (@sjue_bsjs_45955e4c6361540).</description>
    <link>https://dev.to/sjue_bsjs_45955e4c6361540</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3714359%2F368b57eb-478a-4355-b59c-e73152973e7e.png</url>
      <title>DEV Community: Sjue Bsjs</title>
      <link>https://dev.to/sjue_bsjs_45955e4c6361540</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sjue_bsjs_45955e4c6361540"/>
    <language>en</language>
    <item>
      <title>Variables and Constants</title>
      <dc:creator>Sjue Bsjs</dc:creator>
      <pubDate>Mon, 09 Feb 2026 12:49:05 +0000</pubDate>
      <link>https://dev.to/sjue_bsjs_45955e4c6361540/variables-and-constants-42f8</link>
      <guid>https://dev.to/sjue_bsjs_45955e4c6361540/variables-and-constants-42f8</guid>
      <description>&lt;p&gt;As in every programming language, variables are used to store values. Constants are variables whose values don't change.&lt;/p&gt;

&lt;h2&gt;
  
  
  Declaring Variables
&lt;/h2&gt;

&lt;p&gt;In Java, every variable has a &lt;em&gt;type&lt;/em&gt;. You declare a variable by placing the type first, followed by the name of the variable.&lt;/p&gt;

&lt;p&gt;Here are some examples:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;double salary;&lt;br&gt;
int vacationDays;&lt;br&gt;
long earthPopulation;&lt;br&gt;
boolean done;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Notice the semicolon at the end of each declaration. The semicolon is necessary because a declaration is a complete Java statement, which must end in a semicolon.&lt;/p&gt;

&lt;p&gt;The identifier for a variable name (as well as for other names) is made up of letters, digits, currency symbols, and “punctuation connectors.” The first character cannot be a digit.&lt;/p&gt;

&lt;p&gt;Symbols like &lt;em&gt;'+'&lt;/em&gt; or &lt;em&gt;'©'&lt;/em&gt; cannot be used inside variable names, nor can spaces. Letter case is significant: &lt;em&gt;main&lt;/em&gt; and &lt;em&gt;Main&lt;/em&gt; are distinct identifiers. The length of an identifier is essentially unlimited.&lt;/p&gt;

&lt;p&gt;The terms “letter,” “digit,” and “currency symbol” are much broader in Java than in most languages. A letter is &lt;em&gt;any&lt;/em&gt; Unicode character that denotes a letter in a language. For example, German users can use umlauts such as &lt;em&gt;ä&lt;/em&gt; in variable names; Greek speakers could use a &lt;em&gt;π&lt;/em&gt;. Similarly, digits are &lt;em&gt;0–9&lt;/em&gt; and &lt;em&gt;any&lt;/em&gt; Unicode characters that denote a digit. Currency symbols are &lt;em&gt;$&lt;/em&gt;, &lt;em&gt;€&lt;/em&gt;, &lt;em&gt;¥&lt;/em&gt;, and so on. Punctuation connectors include the underscore character &lt;em&gt;, a “wavy low line” ﹏, and a few others. In practice, most programmers stick to _A-Z&lt;/em&gt;, &lt;em&gt;a-z&lt;/em&gt;, &lt;em&gt;0-9&lt;/em&gt;, and the underscore _.&lt;/p&gt;

&lt;p&gt;If you are really curious as to what Unicode characters can be used in identifiers, you can use the &lt;em&gt;isJavaIdentifierStart&lt;/em&gt; and &lt;em&gt;isJavaIdentifierPart&lt;/em&gt; methods in the Character class to check.&lt;/p&gt;

&lt;p&gt;Even though &lt;em&gt;$&lt;/em&gt; is a valid character in an identifier, you should not use it in your own code. It is intended for names that are generated by the Java compiler and other tools.&lt;/p&gt;

&lt;p&gt;You also cannot use a Java keyword such as &lt;em&gt;class&lt;/em&gt; as a variable name.&lt;/p&gt;

&lt;p&gt;Underscores can be parts of identifiers. This is common for constant names, such as &lt;em&gt;Double.POSITIVE_INFINITY&lt;/em&gt;. However, a single underscore _ is a reserved word.&lt;/p&gt;

&lt;p&gt;As of Java 21, a single underscore _ denotes a variable that is syntactially required but never used.&lt;/p&gt;

&lt;p&gt;You can declare multiple variables on a single line:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;int i, j; // both are integers&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I don’t recommend this style. If you declare each variable separately, your programs are easier to read.&lt;/p&gt;

&lt;p&gt;As you saw, names are case sensitive, for example, &lt;em&gt;hireday&lt;/em&gt; and &lt;em&gt;hireDay&lt;/em&gt; are two separate names. In general, you should not have two names that only differ in their letter case. However, sometimes it is difficult to come up with a good name for a variable. Many programmers then give the variable the same name as the type, for example&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Box box; // "Box" is the type and "box" is the&lt;br&gt;
variable name&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Other programmers prefer to use an “a” prefix for the variable:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Box aBox;&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Initializing Variables
&lt;/h2&gt;

&lt;p&gt;After you declare a variable, you must explicitly initialize it by means of an assignment statement, you can never use the value of an uninitialized variable. For example, the Java compiler flags the following sequence of statements as an error:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;int vacationDays;&lt;br&gt;
System.out.println(vacationDays); // ERROR--variable not initialized&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You assign to a previously declared variable by using the variable name on the left, an equal sign (&lt;em&gt;=&lt;/em&gt;), and then some Java expression with an appropriate value on the right.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;int vacationDays;&lt;br&gt;
vacationDays = 12;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You can both declare and initialize a variable on the same line. For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;int vacationDays = 12;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Finally, in Java you can put declarations anywhere in your code. For example, the following is valid code in Java:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;double salary = 65000.0;&lt;br&gt;
System.out.println(salary);&lt;br&gt;
int vacationDays = 12; // OK to declare a variable here&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In Java, it is considered good style to declare variables as closely as possible to the point where they are first used.&lt;/p&gt;

&lt;p&gt;You do not need to declare the types of local variables if they can be inferred from the initial value. Simply use the keyword &lt;em&gt;var&lt;/em&gt; instead of the type:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;var vacationDays = 12; // vacationDays is an int&lt;br&gt;
var greeting = "Hello"; // greeting is a String&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This is not too important for number and string types, but, as you will see in the next chapter, this feature can make the declaration of objects less verbose.&lt;/p&gt;

&lt;p&gt;C and C++ distinguish between the &lt;em&gt;declaration&lt;/em&gt; and &lt;em&gt;definition&lt;/em&gt; of a variable. For example,&lt;/p&gt;

&lt;p&gt;&lt;code&gt;int i = 10;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;is a definition, whereas&lt;/p&gt;

&lt;p&gt;&lt;code&gt;extern int i;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;is a declaration. In Java, no declarations are separate from definitions.&lt;/p&gt;
&lt;h2&gt;
  
  
  Constants
&lt;/h2&gt;

&lt;p&gt;In Java, you use the keyword &lt;em&gt;final&lt;/em&gt; to denote a constant. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Constants
{
public static void main(String[] args)
{
final double CM_PER_INCH = 2.54;
double paperWidth = 8.5;
double paperHeight = 11;
System.out.println("Paper size in centimeters: "
+ paperWidth * CM_PER_INCH + " by " + paperHeight
* CM_PER_INCH);
}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The keyword &lt;em&gt;final&lt;/em&gt; indicates that you can assign to the variable once, and then its value is set once and for all. It is customary to name constants in all uppercase.&lt;/p&gt;

&lt;p&gt;It is probably more common in Java to create a constant so it's available to multiple methods inside a single class. These are usually called &lt;em&gt;class constants&lt;/em&gt;. Set up a class constant with the keywords &lt;em&gt;static final&lt;/em&gt;. Here is an example of using a class constant:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Constants2
{
public static final double CM_PER_INCH = 2.54;
public static void main(String[] args)
{
double paperWidth = 8.5;
double paperHeight = 11;
System.out.println("Paper size in centimeters: "
+ paperWidth * CM_PER_INCH + " by " + paperHeight
* CM_PER_INCH);
}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that the definition of the class constant appears outside the &lt;em&gt;main&lt;/em&gt; method. Thus, the constant can also be used in other methods of the same class. Furthermore, if the constant is declared, as in this example, &lt;em&gt;public&lt;/em&gt;, methods of other classes can also use it, in our example, as &lt;em&gt;Constants2.CM_PER_INCH&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Some coding style guides state that uppercase letters should only be used for &lt;em&gt;static final&lt;/em&gt; variables. If you need to follow such a style guide, and you have a local constant, decide what is more important to you, the fact that it is local (and lowercase), or that it is visibly a constant (&lt;em&gt;static&lt;/em&gt; and uppercase).&lt;/p&gt;

&lt;p&gt;&lt;em&gt;const&lt;/em&gt; is a Java keyword, but it is not currently used for anything. You must use &lt;em&gt;final&lt;/em&gt; for a constant.&lt;/p&gt;

&lt;h2&gt;
  
  
  Enumerated Types
&lt;/h2&gt;

&lt;p&gt;Sometimes, a variable should only hold a restricted set of values. For example, you may sell clothes or pizza in four sizes: small, medium, large, and extra large. Of course, you could encode these sizes as integers &lt;em&gt;1&lt;/em&gt;, &lt;em&gt;2&lt;/em&gt;, &lt;em&gt;3&lt;/em&gt;, &lt;em&gt;4&lt;/em&gt; or characters &lt;em&gt;S&lt;/em&gt;, &lt;em&gt;M&lt;/em&gt;, &lt;em&gt;L&lt;/em&gt;, and &lt;em&gt;X&lt;/em&gt;. But that is an error-prone setup. It is too easy for a variable to hold a wrong value (such as &lt;em&gt;0&lt;/em&gt; or &lt;em&gt;m&lt;/em&gt;).&lt;/p&gt;

&lt;p&gt;You can define your own &lt;em&gt;enumerated type&lt;/em&gt; whenever such a situation arises. An enumerated type has a finite number of named values. For example,&lt;/p&gt;

&lt;p&gt;&lt;code&gt;enum Size { SMALL, MEDIUM, LARGE, EXTRA_LARGE };&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now you can declare variables of this type:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Size s = Size.MEDIUM;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;A variable of type &lt;em&gt;Size&lt;/em&gt; can hold only one of the values listed in the type declaration, or the special value &lt;em&gt;null&lt;/em&gt; that indicates that the variable is not set to any value at all.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>java</category>
      <category>learning</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Simple Java Program, Comments and Data Types</title>
      <dc:creator>Sjue Bsjs</dc:creator>
      <pubDate>Wed, 04 Feb 2026 12:54:14 +0000</pubDate>
      <link>https://dev.to/sjue_bsjs_45955e4c6361540/fundamental-programming-structures-in-java-h07</link>
      <guid>https://dev.to/sjue_bsjs_45955e4c6361540/fundamental-programming-structures-in-java-h07</guid>
      <description>&lt;h2&gt;
  
  
  A Simple Java Program
&lt;/h2&gt;

&lt;p&gt;Let’s look more closely at one of the simplest Java programs you can have one that merely prints a message to console:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;public class FirstSample&lt;br&gt;
{&lt;br&gt;
public static void main(String[] args)&lt;br&gt;
{&lt;br&gt;
System.out.println("We will not use 'Hello,&lt;br&gt;
World!'");&lt;br&gt;
}&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It is worth spending all the time you need to become comfortable with the framework of this sample; the pieces will recur in all applications. First and foremost, &lt;em&gt;Java&lt;/em&gt; is &lt;em&gt;case sensitive&lt;/em&gt;. If you made any mistakes in capitalization (such as typing &lt;em&gt;Main&lt;/em&gt; instead of &lt;em&gt;main&lt;/em&gt;), the program will not run.&lt;/p&gt;

&lt;p&gt;Now let’s look at this source code line by line. The keyword &lt;em&gt;public&lt;/em&gt; is called an &lt;em&gt;access modifier&lt;/em&gt;; these modifiers control the level of access other parts of a program have to this code. The keyword &lt;em&gt;class&lt;/em&gt; reminds you that everything in a Java program lives inside a class. For now, think of a class as a container for the program logic that defines the behavior of an application. Classes are the building blocks with which all Java applications are built. &lt;em&gt;Everything&lt;/em&gt; in a Java program must be inside a class.&lt;/p&gt;

&lt;p&gt;Following the keyword &lt;em&gt;class&lt;/em&gt; is the name of the class. The rules for class names in Java are quite generous. Names must begin with a letter, and after that, they can have any combination of letters and digits. The length is essentially unlimited. You cannot use a Java reserved word (such as &lt;em&gt;public&lt;/em&gt; or &lt;em&gt;class&lt;/em&gt;) for a class name.&lt;/p&gt;

&lt;p&gt;The standard naming convention (used in the name &lt;em&gt;FirstSample&lt;/em&gt;) is that class names are nouns that start with an uppercase letter. If a name consists of multiple words, use an initial uppercase letter in each of the words. This use of uppercase letters in the middle of a name is sometimes called “camel case” or, self-referentially, “CamelCase.”&lt;/p&gt;

&lt;p&gt;You need to make the file name for the source code the same as the name of the public class, with the extension &lt;em&gt;.java&lt;/em&gt; appended. Thus, you must store this code in a file called &lt;em&gt;FirstSample.java&lt;/em&gt;. (Again, case is important—don’t use &lt;em&gt;firstsample.java&lt;/em&gt;.)&lt;/p&gt;

&lt;p&gt;You compile the file with the command&lt;/p&gt;

&lt;p&gt;&lt;code&gt;javac FirstSample.java&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If you have named the file correctly and not made any typos in the source code, you end up with a file containing the bytecodes for this class. The Java compiler names the bytecode file &lt;em&gt;FirstSample.class&lt;/em&gt; and stores it in the same directory as the source file. Finally, launch the program by issuing the following command:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;java FirstSample&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;(Remember to leave off the &lt;em&gt;.class&lt;/em&gt; extension.) When the program executes, it simply displays the string &lt;em&gt;We will not use 'Hello, World!'&lt;/em&gt; on the console. When you use &lt;/p&gt;

&lt;p&gt;&lt;code&gt;java ClassName&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;to run a compiled program, the Java virtual machine always starts execution with the code in the &lt;em&gt;main&lt;/em&gt; method in the class you indicate. (The term “method” is Java-speak for a function.) Thus, you &lt;strong&gt;must&lt;/strong&gt; have a &lt;em&gt;main&lt;/em&gt; method in the source of your class for your code to execute. You can, of course, add your own methods to a class and call them from the &lt;em&gt;main&lt;/em&gt; method.&lt;/p&gt;

&lt;p&gt;Notice the braces &lt;em&gt;{ }&lt;/em&gt; in the source code. In Java, as in C/C++, braces delineate the parts (usually called &lt;em&gt;blocks&lt;/em&gt;) in your program. In Java, the code for any method must be started by an opening brace &lt;em&gt;{&lt;/em&gt; and ended by a closing brace &lt;em&gt;}&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Brace styles have inspired an inordinate amount of useless controversy. As whitespace is irrelevant to the Java compiler, you can use whatever brace style you like.&lt;/p&gt;

&lt;p&gt;For now, don’t worry about the keywords &lt;em&gt;static void&lt;/em&gt; just think of them as part of what you need to get a Java program to compile. The point to remember for now is that every Java application must have a &lt;em&gt;main&lt;/em&gt; method that is declared in the following way:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;public class ClassName&lt;br&gt;
{&lt;br&gt;
public static void main(String[] args)&lt;br&gt;
{&lt;br&gt;
program statements&lt;br&gt;
}&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The byzantine complexity of &lt;em&gt;public static void main&lt;/em&gt; is about to give way to something simpler. Java 21 has a preview feature, described in Java Enhancement Proposal (JEP) 445, that allows you to write the &lt;em&gt;main&lt;/em&gt; method like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;class FirstSample&lt;br&gt;
{&lt;br&gt;
void main()&lt;br&gt;
{&lt;br&gt;
System.out.println("We will not use 'Hello,&lt;br&gt;
World!'");&lt;br&gt;
}&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;That is nicer. No &lt;em&gt;public&lt;/em&gt;, no &lt;em&gt;static&lt;/em&gt;, no &lt;em&gt;String[] args&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;For now, you need to compile and run the program&lt;br&gt;
with a command-line flag:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;javac --enable-preview --source 21 FirstSample.java&lt;br&gt;
java --enable-preview FirstSample&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Even simpler, since there is a single source file, you&lt;br&gt;
can skip the &lt;em&gt;javac&lt;/em&gt; step:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;java --enable-preview --source 21 FirstSample.java&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;for very simple programs, JEP 445 allows you to omit the class, provided, of course, there is only one. The file &lt;em&gt;FirstSample.java&lt;/em&gt; can simply contain:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;void main()&lt;br&gt;
{&lt;br&gt;
System.out.println("We will not use 'Hello,&lt;br&gt;
World!'");&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Since these features are still in preview, we won't use them, but in the future, the first sample program will be a bit simpler.&lt;/p&gt;

&lt;p&gt;As a C++ programmer, you know what a class is. Java classes are similar to C++ classes, but there are a few differences that can trap you. For example, in Java &lt;em&gt;all&lt;/em&gt; functions are methods of some class. (The standard terminology refers to them as methods, not member functions.) Thus, in Java you must have a shell class for the &lt;em&gt;main&lt;/em&gt; method. You may also be familiar with the idea of &lt;em&gt;static member functions&lt;/em&gt; in C++. These are member functions defined inside a class that do not operate on objects.&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;main&lt;/em&gt; method in Java is always static. Finally, as in C/C++, the &lt;em&gt;void&lt;/em&gt; keyword indicates that this method does not return a value. Unlike C/C++, the &lt;em&gt;main&lt;/em&gt; method does not return an “exit code” to the operating system. If the &lt;em&gt;main&lt;/em&gt; method exits normally, the Java program has the exit code &lt;em&gt;0&lt;/em&gt;, indicating successful completion. To terminate the program with a different exit code, call &lt;em&gt;System.exit(code)&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Now that you have seen the basic structure of all Java&lt;br&gt;
programs, turn your attention to the contents of the &lt;em&gt;main&lt;/em&gt; method:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{&lt;br&gt;
System.out.println("We will not use 'Hello, World!'");&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Braces mark the beginning and end of the &lt;em&gt;body&lt;/em&gt; of the method. This method has only one statement in it. As with most programming languages, you can think of Java&lt;br&gt;
statements as sentences of the language. In Java, every statement must end with a semicolon. In particular, carriage returns do not mark the end of a statement, so statements can span multiple lines if need be.&lt;/p&gt;

&lt;p&gt;The body of the &lt;em&gt;main&lt;/em&gt; method contains a statement that outputs a single line of text to the console.&lt;/p&gt;

&lt;p&gt;Here, we are using the &lt;em&gt;System.out&lt;/em&gt; object and calling its &lt;em&gt;println&lt;/em&gt; method. Notice the periods used to invoke a method. Java uses the general syntax&lt;/p&gt;

&lt;p&gt;&lt;code&gt;object.method(arguments)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;as its equivalent of a function call.&lt;/p&gt;

&lt;p&gt;In this case, the &lt;em&gt;println&lt;/em&gt; method receives a string argument. The method displays the string argument on the console. It then terminates the output line, so that each call to &lt;em&gt;println&lt;/em&gt; displays its output on a new line. Notice that Java, like C/C++, uses double quotes to delimit strings.&lt;/p&gt;

&lt;p&gt;Methods in Java, like functions in any programming language, can use zero, one, or more &lt;em&gt;arguments&lt;/em&gt;. Even if a method has no arguments, you must still use empty&lt;br&gt;
parentheses. For example, a variant of the &lt;em&gt;println&lt;/em&gt; method with no arguments just prints a blank line. You invoke it with the call&lt;/p&gt;

&lt;p&gt;&lt;code&gt;System.out.println();&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;System.out&lt;/em&gt; also has a &lt;em&gt;print&lt;/em&gt; method that doesn’t add a newline character to the output. For example, &lt;em&gt;System.out.print("Hello")&lt;/em&gt; prints &lt;em&gt;Hello&lt;/em&gt; without a newline. The next output appears immediately after the letter &lt;em&gt;o&lt;/em&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  Comments
&lt;/h2&gt;

&lt;p&gt;Comments in Java, as in most programming languages, do not show up in the executable program. Thus, you can add as many comments as needed without fear of bloating the code. Java has three ways of marking comments. The most common form is a //. Use this for a comment that runs from the // to the end of the line.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;System.out.println("We will not use 'Hello, World!'"); // is this too cute?&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;When longer comments are needed, you can mark each line with a //, or you can use the /* and &lt;em&gt;/ comment delimiters that let you block off a longer comment.&lt;br&gt;
Finally, a third kind of comment is used to generate documentation automatically. This comment uses a /&lt;/em&gt;* to start and a */ to end.&lt;/p&gt;

&lt;p&gt;/* &lt;em&gt;/ comments do not nest in Java. That is, you might not be able to deactivate code simply by surrounding it with /&lt;/em&gt; and */ because the code you want to deactivate might itself contain a */ delimiter.&lt;/p&gt;
&lt;h2&gt;
  
  
  Data Types
&lt;/h2&gt;

&lt;p&gt;Java is a &lt;em&gt;strongly typed language&lt;/em&gt;. This means that every variable must have a declared type. There are eight &lt;em&gt;primitive&lt;/em&gt; types in Java. Four of them are integer types; two are floating-point number types; one is the character type &lt;em&gt;char&lt;/em&gt;, used for UTF-16 code units in the Unicode encoding scheme; and one is a &lt;em&gt;boolean&lt;/em&gt; type for truth values.&lt;/p&gt;

&lt;p&gt;Java has an arbitrary-precision arithmetic package. However, “big numbers,” as they are called, are Java &lt;em&gt;objects&lt;/em&gt; and not a primitive Java type.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Integer Types&lt;/strong&gt;&lt;br&gt;
The integer types are for numbers without fractional parts. Negative values are allowed. Java provides the four integer types:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;byte 1 byte –128 to 127
short 2 bytes –32,768 to 32,767
int 4 bytes –2,147,483,648 to 2,147,483,647 (just over 2 billion)
long 8 bytes –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In most situations, the &lt;em&gt;int&lt;/em&gt; type is the most practical. If you want to represent the number of inhabitants of our planet, you’ll need to resort to a &lt;em&gt;long&lt;/em&gt;. The &lt;em&gt;byte&lt;/em&gt; and &lt;em&gt;short&lt;/em&gt; types are mainly intended for specialized applications, such as low-level file handling, or for large arrays when storage space is at a premium.&lt;/p&gt;

&lt;p&gt;Under Java, the ranges of the integer types do not depend on the machine on which you will be running the Java code. This alleviates a major pain for the programmer who wants to move software from one platform to another, or even between operating systems on the same platform. In contrast, C and C++ programs use the most efficient integer type for each processor. As a result, a C program that runs well on a 32-bit processor may exhibit integer overflow on a 16-bit system. Since Java programs must run with the same results on all machines, the ranges for the various types are fixed.&lt;/p&gt;

&lt;p&gt;Long integer numbers have a suffix &lt;em&gt;L&lt;/em&gt; or &lt;em&gt;l&lt;/em&gt; (for example, &lt;em&gt;4000000000L&lt;/em&gt;). Hexadecimal numbers have a prefix &lt;em&gt;0x&lt;/em&gt; or &lt;em&gt;0X&lt;/em&gt; (for example, &lt;em&gt;0xCAFE&lt;/em&gt;). Octal numbers have a prefix &lt;em&gt;0&lt;/em&gt; (for example, &lt;em&gt;010&lt;/em&gt; is 8)—naturally, this can be confusing, and few programmers use octal constants.&lt;/p&gt;

&lt;p&gt;You can write numbers in binary, with a prefix &lt;em&gt;0b&lt;/em&gt; or &lt;em&gt;0B&lt;/em&gt;. For example, &lt;em&gt;0b1001&lt;/em&gt; is 9. You can add underscores to number literals, such as &lt;em&gt;1_000_000&lt;/em&gt; (or &lt;em&gt;0b1111_0100_0010_0100_0000&lt;/em&gt;) to denote one million. The underscores are for human eyes only. The Java compiler simply removes them.&lt;/p&gt;

&lt;p&gt;In C and C++, the sizes of types such as &lt;em&gt;int&lt;/em&gt; and &lt;em&gt;long&lt;/em&gt; depend on the target platform. On a 16-bit processor such as the 8086, integers are 2 bytes, but on a 32-bit processor like a Pentium or SPARC they are 4-byte quantities. Similarly, &lt;em&gt;long&lt;/em&gt; values are 4-byte on 32-bit processors and 8-byte on 64-bit processors. These differences make it challenging to write cross-platform programs. In Java, the sizes of all numeric types are platform-independent.&lt;/p&gt;

&lt;p&gt;Note that Java does not have any &lt;em&gt;unsigned&lt;/em&gt; versions of the &lt;em&gt;int&lt;/em&gt;, &lt;em&gt;long&lt;/em&gt;, &lt;em&gt;short&lt;/em&gt;, or &lt;em&gt;byte&lt;/em&gt; types. If you work with integer values that can never be negative and you really need an additional bit, you can, with some care, interpret signed integer values as unsigned. For example, instead of having a &lt;em&gt;byte&lt;/em&gt; value &lt;em&gt;b&lt;/em&gt; represent the range from –128 to 127, you may want a range from 0 to 255. You can store it in a &lt;em&gt;byte&lt;/em&gt;. Due to the nature of binary arithmetic, addition, subtraction, and multiplication will work provided they don't overflow. For other operations, call &lt;em&gt;Byte.toUnsignedInt(b)&lt;/em&gt; to get an &lt;em&gt;int&lt;/em&gt; value between 0 and 255, then process the integer value and cast back to &lt;em&gt;byte&lt;/em&gt;. The &lt;em&gt;Integer&lt;/em&gt; and &lt;em&gt;Long&lt;/em&gt; classes have methods for unsigned division and remainder.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Floating-Point Types&lt;/strong&gt;&lt;br&gt;
The floating-point types denote numbers with fractional parts. The two floating-point types:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;float 4 bytes Approximately ±3.40282347 ×10^38 (6–7 significant decimal digits)
double 8 bytes Approximately ±1.79769313486231570 ×10^308 (15 significant decimal digits)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The name &lt;em&gt;double&lt;/em&gt; refers to the fact that these numbers have twice the precision of the &lt;em&gt;float&lt;/em&gt; type. (Some people call these &lt;em&gt;double-precision&lt;/em&gt; numbers.) The limited precision of &lt;em&gt;float&lt;/em&gt; (6-7 significant digits) is simply not sufficient for many situations. Use &lt;em&gt;float&lt;/em&gt; values only when you work with a library that requires them, or when you need to store a very large number of them.&lt;/p&gt;

&lt;p&gt;Java 20 adds a couple of methods (&lt;em&gt;Float.floatToFloat16&lt;/em&gt; and &lt;em&gt;Float.float16toFloat&lt;/em&gt;) for storing “half-precision” 16-bit floating-point numbers in &lt;em&gt;short&lt;/em&gt; values. These are used for implementating neural networks.&lt;/p&gt;

&lt;p&gt;Numbers of type &lt;em&gt;float&lt;/em&gt; have a suffix &lt;em&gt;F&lt;/em&gt; or &lt;em&gt;f&lt;/em&gt; (for example, &lt;em&gt;3.14F&lt;/em&gt;). Floating-point numbers without an &lt;em&gt;F&lt;/em&gt; suffix (such as &lt;em&gt;3.14&lt;/em&gt;) are always considered to be of type &lt;em&gt;double&lt;/em&gt;. You can optionally supply the &lt;em&gt;D&lt;/em&gt; or &lt;em&gt;d&lt;/em&gt; suffix (for example, &lt;em&gt;3.14D&lt;/em&gt;). An &lt;em&gt;E&lt;/em&gt; or &lt;em&gt;e&lt;/em&gt; denotes a decimal exponent. For example, &lt;em&gt;1.729E3&lt;/em&gt; is the same as &lt;em&gt;1729&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;You can specify floating-point literals in hexadecimal. For example, 0.125 = 2^–3 can be written as &lt;em&gt;0x1.0p-3&lt;/em&gt;. In hexadecimal notation, you use a &lt;em&gt;p&lt;/em&gt;, not an &lt;em&gt;e&lt;/em&gt;, to denote the exponent. (An &lt;em&gt;e&lt;/em&gt; is a hexadecimal digit.) Note that the mantissa is written in hexadecimal and the exponent in decimal. The base of the exponent is 2, not 10.&lt;/p&gt;

&lt;p&gt;All floating-point computations follow the IEEE 754 specification. In particular, there are three special floating-point values to denote overflows and errors:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Positive infinity&lt;/li&gt;
&lt;li&gt;Negative infinity&lt;/li&gt;
&lt;li&gt;NaN (not a number)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, the result of dividing a positive floating-point number by 0 is positive infinity. Dividing 0.0 by 0 or the square root of a negative number yields NaN.&lt;/p&gt;

&lt;p&gt;The constants &lt;em&gt;Double.POSITIVE_INFINITY&lt;/em&gt;, &lt;em&gt;Double.NEGATIVE_INFINITY&lt;/em&gt;, and &lt;em&gt;Double.NaN&lt;/em&gt; (as well as corresponding &lt;em&gt;Float&lt;/em&gt; constants) represent these special values, but they are rarely used in practice. In particular, you cannot test&lt;/p&gt;

&lt;p&gt;&lt;code&gt;if (x == Double.NaN) // is never true&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;to check whether a particular result equals &lt;em&gt;Double.NaN&lt;/em&gt;. All “not a number” values are considered&lt;br&gt;
distinct. However, you can use the &lt;em&gt;Double.isNaN&lt;/em&gt; method:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;if (Double.isNaN(x)) // check whether x is "not a&lt;br&gt;
number"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Floating-point numbers are not suitable for financial calculations in which roundoff errors cannot be tolerated. For example, the command &lt;em&gt;System.out.println(2.0 - 1.1)&lt;/em&gt; prints &lt;em&gt;0.8999999999999999&lt;/em&gt;, not &lt;em&gt;0.9&lt;/em&gt; as you would expect. Such roundoff errors are caused by the fact that floating-point numbers are represented in the binary number system. There is no precise binary representation of the fraction 1/10, just as there is no accurate representation of the fraction 1/3 in the decimal system. If you need precise numerical computations without roundoff errors, use the &lt;em&gt;BigDecimal&lt;/em&gt; class.&lt;/p&gt;
&lt;h2&gt;
  
  
  The char Type
&lt;/h2&gt;

&lt;p&gt;The &lt;em&gt;char&lt;/em&gt; type was originally intended to describe individual characters. However, this is no longer the case. Nowadays, some Unicode characters can be described with one &lt;em&gt;char&lt;/em&gt; value, and other Unicode characters require two &lt;em&gt;char&lt;/em&gt; values.&lt;/p&gt;

&lt;p&gt;Literal values of type &lt;em&gt;char&lt;/em&gt; are enclosed in single quotes. For example, &lt;em&gt;'A'&lt;/em&gt; is a character constant with value 65. It is different from &lt;em&gt;"A"&lt;/em&gt;, a string containing a single character. Values of type &lt;em&gt;char&lt;/em&gt; can be expressed as hexadecimal values that run from &lt;em&gt;\u0000&lt;/em&gt; to &lt;em&gt;\uFFFF&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Besides the &lt;em&gt;\u&lt;/em&gt; escape sequences, there are several escape sequences for special characters. You can use these escape sequences inside quoted character literals and strings, such as &lt;em&gt;'\u005B'&lt;/em&gt; or &lt;em&gt;"Hello\n"&lt;/em&gt;. The &lt;em&gt;\u&lt;/em&gt; escape sequence (but none of the other escape sequences) can even be used _outside _quoted character constants and strings. For example,&lt;/p&gt;

&lt;p&gt;&lt;code&gt;public static void main(String\u005B\u005D args)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;is perfectly legal &lt;em&gt;\u005B&lt;/em&gt; and &lt;em&gt;\u005D&lt;/em&gt; are the encodings for &lt;em&gt;[&lt;/em&gt; and &lt;em&gt;]&lt;/em&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;\b Backspace \u0008
\t Tab \u0009
\n Line feed \u000a
\r Carriage return \u000d
\f Form feed \u000c
\" Double quote \u0022
\' Single quote \u0027
\\ Backslash \u005c
\s Space. Used in text blocks to retain trailing whitespace. \u0020
\newline In text blocks only: Join this line with the next —
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Unicode escape sequences are processed before the code is parsed. For example, &lt;em&gt;"\u0022+\u0022"&lt;/em&gt; is not a string consisting of a plus sign surrounded by quotation marks (U+0022). Instead, the &lt;em&gt;\u0022&lt;/em&gt; are converted into &lt;em&gt;"&lt;/em&gt; before parsing, yielding &lt;em&gt;""+""&lt;/em&gt;, or an empty string.&lt;/p&gt;

&lt;p&gt;Even more insidiously, you must beware of &lt;em&gt;\u&lt;/em&gt; inside comments. The comment&lt;/p&gt;

&lt;p&gt;&lt;code&gt;// \u000A is a newline&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;yields a syntax error since &lt;em&gt;\u000A&lt;/em&gt; is replaced with a newline when the program is read. Similarly, a comment&lt;/p&gt;

&lt;p&gt;&lt;code&gt;// look inside c:\users&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;yields a syntax error because the &lt;em&gt;\u&lt;/em&gt; is not followed by four hex digits.&lt;/p&gt;

&lt;p&gt;You can have any number of u in a Unicode escape sequence: &lt;em&gt;\u00E9&lt;/em&gt; and &lt;em&gt;\uuu00E9&lt;/em&gt; both denote the character &lt;em&gt;é&lt;/em&gt;. There is a reason for this oddity. Consider a programmer happily coding in Unicode who is forced, for some archaic reason, to check in code as ASCII only. A conversion tool can turn any character &amp;gt; U+007F into a Unicode escape and add a &lt;em&gt;u&lt;/em&gt; to every existing Unicode escape. That makes the conversion reversible. For example, &lt;em&gt;\uD800&lt;/em&gt; &lt;em&gt;é&lt;/em&gt; is turned into &lt;em&gt;\uuD800&lt;/em&gt; &lt;em&gt;\u00E9&lt;/em&gt; and can be converted back to &lt;em&gt;\uD800&lt;/em&gt; &lt;em&gt;é&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Unicode and the char Type
&lt;/h2&gt;

&lt;p&gt;To fully understand the &lt;em&gt;char&lt;/em&gt; type, you have to know about the Unicode encoding scheme. Before Unicode, there were many different character encoding standards: ASCII in the United States, ISO 8859-1 for Western European languages, KOI-8 for Russian, GB18030 and BIG-5 for Chinese, and so on. This caused two problems. First, a particular code value corresponds to different letters in the different encoding schemes. Second, the encodings for languages with large character sets have variable length: Some common characters are encoded as single bytes, others require two or more bytes.&lt;/p&gt;

&lt;p&gt;Unicode was designed to solve both problems. When the&lt;br&gt;
unification effort started in the 1980s, a fixed 2-byte code was more than sufficient to encode all characters used in all languages in the world, with room to spare for future expansion or so everyone thought at the time. In 1991, Unicode 1.0 was released, using slightly less than half of the available 65,536 code values. Java was designed from&lt;br&gt;
the ground up to use 16-bit Unicode characters, which was a major advance over other programming languages that used 8-bit characters.&lt;/p&gt;

&lt;p&gt;Unfortunately, over time, the inevitable happened. Unicode grew beyond 65,536 characters, primarily due to the addition of a very large set of ideographs used for Chinese, Japanese, and Korean. Now, the 16-bit &lt;em&gt;char&lt;/em&gt; type is insufficient to describe all Unicode characters.&lt;/p&gt;

&lt;p&gt;We need a bit of terminology to explain how this problem is resolved in Java. A &lt;em&gt;code point&lt;/em&gt; is an integer value associated with a character in an encoding scheme. In the Unicode standard, code points are written in hexadecimal and prefixed with U+, such as U+0041 for the code point of the Latin letter A. Unicode has code points that are grouped into 17 code planes, each holding 65536 characters. The first code plane, called the &lt;em&gt;basic multilingual plane&lt;/em&gt;, consists of the “classic” Unicode characters with code points U+0000 to U+FFFF. Sixteen additional planes, with code points U+10000 to U+10FFFF, hold many more characters called &lt;em&gt;supplementary&lt;/em&gt; characters.&lt;/p&gt;

&lt;p&gt;How a Unicode code point (that is, an integer ranging from 0 to hexadecimal 10FFF) is represented in bits depends on the &lt;em&gt;character encoding&lt;/em&gt;. You could encode each character as a sequence of 21 bits, but that is impractical for computer hardware. The UTF-32 encoding simply places each code point into 32 bits, where the top 11 bits are zero. That is rather wasteful. The most common encoding on the Internet is UTF-8, using between one and four bytes per character.&lt;/p&gt;

&lt;p&gt;Java strings use the UTF-16 encoding. It encodes all Unicode code points in a variable-length code of 16-bit units, called &lt;em&gt;code units&lt;/em&gt;. The characters in the basic multilingual plane are encoded as a single code unit. All other characters are encoded as consecutive pairs of code units. Each of the code units in such an encoding pair falls into a range of 2048 unused values of the basic multilingual plane, called the &lt;em&gt;surrogates area&lt;/em&gt; ('\uD800' to '\uDBFF' for the first code unit, '\uDC00' to '\uDFFF' for the second code unit). This is rather clever, because you can immediately tell whether a code unit encodes a single character or it is the first or second part of a supplementary character. For example, the beer mug emoji 🍺 has code point U+1F37A and is encoded by the two code units '\uD83C' and '\uDF7A'. Each code unit is stored as a char value. The details are not important. All you need to know is that a single Unicode character may require one or two &lt;em&gt;char&lt;/em&gt; values.&lt;/p&gt;

&lt;p&gt;You cannot ignore characters with code units above U+FFFF. Your customers may well write in a language where these characters are needed, or they may be fond of putting emojis such as 🍺 into their messages.&lt;/p&gt;

&lt;p&gt;Nowadays, Unicode has become so complex that even code&lt;br&gt;
points no longer correspond to what a human viewer would perceive as a single character or symbol. This happens with languages whose characters are made from smaller building blocks, with emojis that can have modifiers for gender and skin tone, and with an ever-growing number of other compositions.&lt;/p&gt;

&lt;p&gt;Consider the ITALY flag. You perceive a single symbol: the flag of Italy. However, this symbol is composed of two Unicode code points: U+1F1EE (regional indicator symbol letter I) and U+1F1F9 (regional indicator symbol letter T). About 250 flags can be formed with these regional indicators. The pirate flag, on the other hand, is composed of U+1F3F4 (waving black flag), U+200D (zero width joiner), U+2620 (skull and crossbones), and U+FE0F (variation selector-16). In Java, you need four &lt;em&gt;char&lt;/em&gt; values to represent the first flag, five for the second.&lt;/p&gt;

&lt;p&gt;In summary, a visible character or symbol is encoded as a sequence of some number of &lt;em&gt;char&lt;/em&gt; values, and there is almost never a need to look at the individual values. Always work with strings and don't worry about their representation as &lt;em&gt;char&lt;/em&gt; sequences.&lt;/p&gt;

&lt;h2&gt;
  
  
  The boolean Type
&lt;/h2&gt;

&lt;p&gt;The &lt;em&gt;boolean&lt;/em&gt; type has two values, &lt;em&gt;false&lt;/em&gt; and &lt;em&gt;true&lt;/em&gt;. It is used for evaluating logical conditions. You cannot convert between integers and &lt;em&gt;boolean&lt;/em&gt; values.&lt;/p&gt;

&lt;p&gt;In C++, numbers and even pointers can be used in place of &lt;em&gt;boolean&lt;/em&gt; values. The value 0 is equivalent to the &lt;em&gt;bool&lt;/em&gt; value &lt;em&gt;false&lt;/em&gt;, and a nonzero value is equivalent to &lt;em&gt;true&lt;/em&gt;. This is not the case in Java. Thus, Java programmers are shielded from accidents such as&lt;/p&gt;

&lt;p&gt;&lt;code&gt;if (x = 0) // oops... meant x == 0&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In C++, this test compiles and runs, always evaluating to &lt;em&gt;false&lt;/em&gt;. In Java, the test does not compile because the integer expression &lt;em&gt;x = 0&lt;/em&gt; cannot be converted to a &lt;em&gt;boolean&lt;/em&gt; value.m&lt;/p&gt;

</description>
      <category>programming</category>
      <category>beginners</category>
      <category>learning</category>
      <category>java</category>
    </item>
    <item>
      <title>The Java Programming Environment</title>
      <dc:creator>Sjue Bsjs</dc:creator>
      <pubDate>Fri, 16 Jan 2026 10:32:46 +0000</pubDate>
      <link>https://dev.to/sjue_bsjs_45955e4c6361540/the-java-programming-environment-399c</link>
      <guid>https://dev.to/sjue_bsjs_45955e4c6361540/the-java-programming-environment-399c</guid>
      <description>&lt;h2&gt;
  
  
  Installing the Java Development Kit
&lt;/h2&gt;

&lt;p&gt;You can download the Java Development Kit from Oracle at &lt;a href="https://www.oracle.com/java/technologies/downloads" rel="noopener noreferrer"&gt;https://www.oracle.com/java/technologies/downloads&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting Up the JDK
&lt;/h2&gt;

&lt;p&gt;After downloading the JDK, you need to install it and figure out where it was installed - you'll need that information later. If you run Windows or have a Mac, simply launch the setup program and choose the default options. On Linux, uncompress the &lt;em&gt;.tar.gz&lt;/em&gt; file to a location of your choice, such as your home directory or &lt;em&gt;/opt&lt;/em&gt;. Then set the &lt;em&gt;PATH&lt;/em&gt; to the &lt;em&gt;bin&lt;/em&gt; subdirectory of the directory into which the JDK was placed, such as &lt;em&gt;/opt/jdk-21.0.4/bin.&lt;/em&gt; This is usually achieved by adding a line such as the following to the end of your &lt;em&gt;~/.bashrc&lt;/em&gt; or &lt;em&gt;~/.bash_profile&lt;/em&gt; file:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;export PATH=/opt/jdk-21.0.4/bin:$PATH&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here is how you test whether you did it right. Start a terminal window. Type the line&lt;/p&gt;

&lt;p&gt;&lt;code&gt;javac --version&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;and press the Enter key. You should get a display such as this one:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;javac 21.0.4&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If instead you get a message such as "javac: command not found" or "The name specified is not recognized as an internal or external command, operable program or batch file," then you need to double-check your installation.&lt;/p&gt;

&lt;p&gt;It is often useful to know where the JDK is installed on your system. The installation directory is denoted as &lt;em&gt;jdk&lt;/em&gt;. For example, when referring to the &lt;em&gt;jdk/bin&lt;/em&gt; directory, I mean the directory such as &lt;em&gt;/opt/jdk-17.0.4/bin&lt;/em&gt; or &lt;em&gt;C:\Program Files\Java\jdk-21\bin.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;On Windows, the Oracle JDK installer adds the directory &lt;em&gt;C:\Program Files\CommonFiles\Oracle\Java\javapath&lt;/em&gt; to the &lt;em&gt;PATH&lt;/em&gt; environment variable. That directory only contains the &lt;em&gt;javac&lt;/em&gt;, &lt;em&gt;javaw&lt;/em&gt;, &lt;em&gt;java&lt;/em&gt;, and &lt;em&gt;jshell&lt;/em&gt; executables. The &lt;em&gt;javaw&lt;/em&gt; executable is a Windows-only feature for launching a program without a console window. The other tools in the Java Development Kit can be found in the &lt;em&gt;bin&lt;/em&gt; subdirectory of the JDK installation directory. When invoking those programs, either specify the complete path (such as &lt;em&gt;C:\Program Files\Java\jdk-21\bin\javadoc&lt;/em&gt; for the &lt;em&gt;javadoc&lt;/em&gt; tool), or add the &lt;em&gt;bin&lt;/em&gt; subdirectory to the &lt;em&gt;PATH&lt;/em&gt; environment variable. One way to achieve this is with the &lt;em&gt;setx&lt;/em&gt; command&lt;/p&gt;

&lt;p&gt;&lt;code&gt;setx PATH "%PATH%;c:\Program Files\Java\jdk-21\bin"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Open another terminal window for the change to take effect.&lt;/p&gt;

&lt;h2&gt;
  
  
  Installing Source Files and Documentation
&lt;/h2&gt;

&lt;p&gt;The library source files are delivered in the JDK as a compressed file &lt;em&gt;jdk/lib/src.zip&lt;/em&gt;. Unpack that file to get access to the source code. Simply do the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Make sure the JDK is installed and the &lt;em&gt;jdk/bin&lt;/em&gt; directory is on the executable path.&lt;/li&gt;
&lt;li&gt;Make a directory &lt;em&gt;javasrc&lt;/em&gt; in your home directory. If you like, you can do this from a terminal window.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;code&gt;mkdir javasrc&lt;/code&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Inside the &lt;em&gt;jdk/lib&lt;/em&gt; directory, locate the file &lt;em&gt;src.zip&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Unzip the &lt;em&gt;src.zip&lt;/em&gt; file into the &lt;em&gt;javasrc&lt;/em&gt; directory. In a terminal window, you can execute the commands&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;code&gt;cd javasrc&lt;br&gt;
jar xvf jdk/lib/src.zip&lt;br&gt;
cd ..&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;src.zip&lt;/em&gt; file contains the source code for all public libraries.&lt;/p&gt;

&lt;p&gt;You can read the JDK documentation at &lt;a href="https://docs.oracle.com/en/java/javase/21/docs/" rel="noopener noreferrer"&gt;https://docs.oracle.com/en/java/javase/21/docs/&lt;/a&gt;. If you prefer to have an offline version, follow these steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Download the documentation zip file from &lt;a href="https://www.oracle.com/java/technologies/downloads" rel="noopener noreferrer"&gt;https://www.oracle.com/java/technologies/downloads&lt;/a&gt;. It is called &lt;em&gt;jdk-21.0.x_doc-all.zip.&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Unzip the file and rename the doc directory into something more descriptive, like &lt;em&gt;jdk-21-docs&lt;/em&gt;. If you like, you can do this from the command line:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;code&gt;jar xvf Downloads/jdk-21.0.x_doc-all.zip&lt;br&gt;
mv docs jdk-21-docs&lt;/code&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;In your browser, navigate to &lt;em&gt;jdk-21-docs/index.html&lt;/em&gt; and add this page to your bookmarks.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You should also install the Core Java program examples. You can download them from &lt;a href="https://horstmann.com/corejava" rel="noopener noreferrer"&gt;https://horstmann.com/corejava&lt;/a&gt;. The programs are packaged into a zip file &lt;em&gt;corejava.zip&lt;/em&gt;. Just unzip them into your home directory. They will be located in a directory &lt;em&gt;corejava&lt;/em&gt;. If you like, you can do this from the command line:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;jar xvf Downloads/corejava.zip&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Using the Command-Line Tools
&lt;/h2&gt;

&lt;p&gt;If your programming experience comes from a development environment such as Microsoft Visual Studio, you are accustomed to a system with a built-in text editor, menus to compile and launch a program, and a debugger. The JDK contains nothing even remotely similar. You do everything by typing in commands in a terminal window. This may sound cumbersome, but it is nevertheless an essential skill. When you first install Java, you will want to troubleshoot your installation before you install a development environment. Moreover, by executing the basic steps&lt;br&gt;
yourself, you gain a better understanding of what a development environment does behind your back.&lt;/p&gt;

&lt;p&gt;However, after you have mastered the basic steps of compiling and running Java programs, you will want to use a professional development environment. You will see how to do that in the following section.&lt;/p&gt;

&lt;p&gt;Let's get started the hard way: compiling and launching a Java program from the command line.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open a terminal window.&lt;/li&gt;
&lt;li&gt;Go to the &lt;em&gt;corejava/v1ch02/Welcome&lt;/em&gt; directory. (The &lt;em&gt;corejava&lt;/em&gt; directory is where you installed the source code for the book examples)&lt;/li&gt;
&lt;li&gt;Enter the following commands:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;code&gt;javac Welcome.java&lt;br&gt;
java Welcome&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In the terminal window, you should see the output below&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Welcome to Core Java!&lt;br&gt;
=====================&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Congratulations! You have just compiled and run your first Java program.&lt;/p&gt;

&lt;p&gt;What happened? The &lt;em&gt;javac&lt;/em&gt; program is the Java compiler. It compiles the file &lt;em&gt;Welcome.java&lt;/em&gt; into the file &lt;em&gt;Welcome.class&lt;/em&gt;. The java program launches the Java virtual machine. It executes the bytecodes that the compiler placed in the class file.&lt;br&gt;
The &lt;em&gt;Welcome&lt;/em&gt; program is extremely simple. It merely prints a message to the terminal. You may enjoy looking inside the program below&lt;/p&gt;

&lt;p&gt;&lt;code&gt;/**&lt;br&gt;
 * This program displays a greeting for the reader.&lt;br&gt;
 */&lt;br&gt;
 public class Welcome&lt;br&gt;
 {&lt;br&gt;
  public static void main(String[] args)&lt;br&gt;
  {&lt;br&gt;
   String greeting = "Welcome to Core Java!";&lt;br&gt;
   System.out.println(greeting);&lt;br&gt;
   for (int i = 0; i &amp;lt; greeting.length(); i++)&lt;br&gt;
    System.out.print("=");&lt;br&gt;
   System.out.println();&lt;br&gt;
   }&lt;br&gt;
 }&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In the age of integrated development environments, many programmers are unfamiliar with running programs in a terminal window. Any number of things can go wrong, leading to frustrating results.&lt;/p&gt;

&lt;p&gt;Pay attention to the following points:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;If you type in the program by hand, make sure you
correctly enter the uppercase and lowercase letters. In particular, the class name is &lt;em&gt;Welcome&lt;/em&gt; and not &lt;em&gt;welcome&lt;/em&gt; or &lt;em&gt;WELCOME&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;The compiler requires a file name (&lt;em&gt;Welcome.java&lt;/em&gt;). When you run the program, you specify a class name (&lt;em&gt;Welcome&lt;/em&gt;) without a &lt;em&gt;.java&lt;/em&gt; or &lt;em&gt;.class&lt;/em&gt; extension.&lt;/li&gt;
&lt;li&gt;If you get a message such as “Bad command or file
name” or “javac: command not found,” go back and double-check your installation, in particular the executable path setting.&lt;/li&gt;
&lt;li&gt;If &lt;em&gt;javac&lt;/em&gt; reports that it cannot find the file &lt;em&gt;Welcome.java&lt;/em&gt;, you should check whether that file is present in the directory.
Under Linux, check that you used the correct capitalization for &lt;em&gt;Welcome.java&lt;/em&gt;.
Under Windows, use the &lt;em&gt;dir&lt;/em&gt; command, not the graphical Explorer tool. Some text editors (in particular Notepad) insist on adding an extension &lt;em&gt;.txt&lt;/em&gt; to every file's name. If you use Notepad to edit &lt;em&gt;Welcome.java&lt;/em&gt;, it will actually save it as Welcome.java.txt. Under the default Windows settings, Explorer conspires with Notepad and hides the .txt extension because it belongs to a “known file type.” In that case, you need to rename the file, or save it again placing quotes around the file name: "&lt;em&gt;Welcome.java&lt;/em&gt;".&lt;/li&gt;
&lt;li&gt;If you launch your program and get an error message
complaining about a &lt;em&gt;java.lang.NoClassDefFoundError&lt;/em&gt;, then carefully check the name of the offending class.
If you get a complaint about &lt;em&gt;welcome&lt;/em&gt; (with a lowercase &lt;em&gt;w&lt;/em&gt;), then you should reissue the &lt;em&gt;java Welcome&lt;/em&gt; command with an uppercase &lt;em&gt;W&lt;/em&gt;. As always, case matters in Java. If you get a complaint about &lt;em&gt;Welcome/java&lt;/em&gt;, it means you accidentally typed &lt;em&gt;java Welcome.java&lt;/em&gt;. Reissue the command as &lt;em&gt;java Welcome&lt;/em&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Using an Integrated Development Environment
&lt;/h2&gt;

&lt;p&gt;These environments are so powerful and convenient that it simply doesn’t make much sense to labor on without them. Excellent choices are the freely available Eclipse, IntelliJ IDEA, and NetBeans. In this section, you will learn how to get started with Eclipse.&lt;/p&gt;

&lt;p&gt;Get started by downloading Eclipse from&lt;br&gt;
&lt;a href="https://eclipse.org/downloads" rel="noopener noreferrer"&gt;https://eclipse.org/downloads&lt;/a&gt;. Versions exist for Linux, MacOS X, and Windows. Run the installation program and pick the installation set called “Eclipse IDE for Java Developers.”&lt;/p&gt;

&lt;p&gt;Here are the steps to write a program with Eclipse:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;After starting Eclipse, select File → New → Project
from the menu.&lt;/li&gt;
&lt;li&gt;Select “Java Project” from the wizard dialog.&lt;/li&gt;
&lt;li&gt;Click the Next button. Uncheck the “Use default location” checkbox. Click on Browse and navigate to
the &lt;em&gt;corejava/v1ch02/Welcome&lt;/em&gt; directory.&lt;/li&gt;
&lt;li&gt;Click the Finish button. The project is now created.&lt;/li&gt;
&lt;li&gt;Click on the triangles in the left pane next to the
project until you locate the file Welcome.java, and double-click on it. You should now see a pane with the program code.&lt;/li&gt;
&lt;li&gt;With the right mouse button, click on the project name (Welcome) in the left pane. Select Run → Run As →
Java Application. The program output is displayed in the console pane.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Presumably, this program does not have typos or bugs. (It was only a few lines of code, after all.) Let us suppose, for the sake of argument, that your code occasionally contains a typo (perhaps even a syntax error). Try it out—ruin your file, for example, by changing the capitalization of String as follows:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;string greeting = "Welcome to Core Java!";&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Note the wiggly line under &lt;em&gt;string&lt;/em&gt;. In the tabs below the source code, click on Problems and expand the triangles until you see an error message that complains about an unknown &lt;em&gt;string&lt;/em&gt; type. Click on the error message. The cursor moves to the matching line in the edit pane, where you can correct your error. This allows you to fix your errors quickly.&lt;/p&gt;

&lt;p&gt;Often, an Eclipse error report is accompanied by a lightbulb icon. Click on the lightbulb to get a list of suggested fixes.&lt;/p&gt;

&lt;h2&gt;
  
  
  JShell
&lt;/h2&gt;

&lt;p&gt;The JShell program provides a “read-evaluate-print loop,” or REPL. You type a Java expression; JShell evaluates your input, prints the result, and waits for your next input. This is an excellent way to experiment—much faster than writing a complete program in an integrated development environment.&lt;/p&gt;

&lt;p&gt;To start JShell, simply type &lt;em&gt;jshell&lt;/em&gt; in a terminal window.&lt;/p&gt;

&lt;p&gt;JShell starts with a greeting, followed by a prompt:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;| Welcome to JShell -- Version 21.0.4&lt;br&gt;
| For an introduction type: /help intro&lt;br&gt;
jshell&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now type an expression, such as&lt;/p&gt;

&lt;p&gt;&lt;code&gt;"Core Java".length()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;JShell responds with the result—in this case, the number of characters in the string “Core Java”.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$1 ==&amp;gt; 9&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Note that you do not type &lt;em&gt;System.out.println&lt;/em&gt;. JShell&lt;br&gt;
automatically prints the value of every expression that you enter.&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;$1&lt;/em&gt; in the output indicates that the result is available in further calculations. For example, if you type&lt;/p&gt;

&lt;p&gt;&lt;code&gt;5 * $1 - 3&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;the response is&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$2 ==&amp;gt; 42&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If you need a variable many times, you can give it a more memorable name. For example,&lt;/p&gt;

&lt;p&gt;&lt;code&gt;jshell&amp;gt; var answer = 6 * 7&lt;br&gt;
answer ==&amp;gt; 42&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Another useful feature is tab completion. Type&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Math.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;followed by the Tab key. Because there are so many&lt;br&gt;
completions, you are prompted to hit the Tab key again. You get a list of all methods that you can invoke with the &lt;em&gt;Math&lt;/em&gt; class:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;jshell&amp;gt; Math.&lt;br&gt;
E IEEEremainder( PI&lt;br&gt;
TAU abs( absExact(&lt;br&gt;
acos( addExact( asin(&lt;br&gt;
atan( atan2( cbrt(&lt;br&gt;
ceil( ceilDiv(&lt;br&gt;
ceilDivExact(&lt;br&gt;
ceilMod( clamp( class&lt;br&gt;
copySign( cos( cosh(&lt;br&gt;
decrementExact( divideExact( exp(&lt;br&gt;
expm1( floor( floorDiv(&lt;br&gt;
floorDivExact( floorMod( fma(&lt;br&gt;
getExponent( hypot(&lt;br&gt;
incrementExact(&lt;br&gt;
log( log10( log1p(&lt;br&gt;
max( min(&lt;br&gt;
multiplyExact(&lt;br&gt;
multiplyFull( multiplyHigh(&lt;br&gt;
negateExact(&lt;br&gt;
nextAfter( nextDown( nextUp(&lt;br&gt;
pow( random() rint(&lt;br&gt;
round( scalb( signum(&lt;br&gt;
sin( sinh( sqrt(&lt;br&gt;
subtractExact( tan( tanh(&lt;br&gt;
toDegrees( toIntExact( toRadians(&lt;br&gt;
ulp( unsignedMultiplyHigh(&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now type &lt;em&gt;l&lt;/em&gt; and hit the Tab key again. The method name is completed to &lt;em&gt;log&lt;/em&gt;, and you get a shorter list: &lt;/p&gt;

&lt;p&gt;&lt;code&gt;jshell&amp;gt; Math.log&lt;br&gt;
log( log10( log1p(&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now you can fill in the rest by hand:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;jshell&amp;gt; Math.log10(0.001)&lt;br&gt;
$4 ==&amp;gt; -3.0&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To repeat a command, hit the ↑ key until you see the line that you want to reissue or edit. You can move the cursor in the line with the ← and → keys, and add or delete characters. Hit Enter when you are done. For example, hit the ↑ key and replace 0.001 with 1000, then hit Enter:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;jshell&amp;gt; Math.log10(1000)&lt;br&gt;
$5 ==&amp;gt; 3.0&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You exit JShell with the command&lt;/p&gt;

&lt;p&gt;&lt;code&gt;/exit&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;JShell makes it easy and fun to learn the Java language and library without having to launch a heavy-duty development environment and without fussing with a program and a &lt;em&gt;main&lt;/em&gt; method.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>beginners</category>
      <category>learning</category>
      <category>java</category>
    </item>
  </channel>
</rss>
