Knowledge in Java

Java Comments

Java CommentsComments can be used to explain Java code, and to make it more readable. It can also be used to prevent execution when testing alternative code.Single-line comments start with two forward slashes (//).Any text between // and the end of the line is ignored by Java (will not be executed).This example uses a single-line comment before a line of code:Example// This is a comment System.out.println("Hello World"); This example uses a single-line comment at the end of a line of code:ExampleSystem.out.println("Hello World"); // This is a comment Java Multi-line CommentsMulti-line comments start with /* and ends with */.Any text between /* and */ will be ignored by Java.This example uses a multi-line comment (a comment block) to explain the code:Example/* The code below will print the words Hello World to the screen, and it is amazing */ System.out.println("Hello World");

Java Variables

Java VariablesVariables are containers for storing data values.In Java, there are different types of variables, for example:String - stores text, such as "Hello". String values are surrounded by double quotesint - stores integers (whole numbers), without decimals, such as 123 or -123float - stores floating point numbers, with decimals, such as 19.99 or -19.99char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotesboolean - stores values with two states: true or falseDeclaring (Creating) VariablesTo create a variable, you must specify the type and assign it a value:Syntaxtype variable = value; Where type is one of Java's types (such as int or String), and variable is the name of the variable (such as x or name). The equal sign is used to assign values to the variable.To create a variable that should store text, look at the following example:ExampleCreate a variable called name of type String and assign it the value "John":String name = "John"; System.out.println(name); To create a variable that should store a number, look at the following example:ExampleCreate a variable called myNum of type int and assign it the value 15:int myNum = 15; System.out.println(myNum); You can also declare a variable without assigning the value, and assign the value later:Exampleint myNum; myNum = 15; System.out.println(myNum); A demonstration of how to declare variables of other types:Exampleint myNum = 5; float myFloatNum = 5.99f; char myLetter = 'D'; boolean myBool = true; String myText = "Hello";

Java Identifiers

Java IdentifiersAll Java variables must be identified with unique names.These unique names are called identifiers.Identifiers can be short names (like x and y) or more descriptive names (age, sum, totalVolume).Note: It is recommended to use descriptive names in order to create understandable and maintainable code.The general rules for constructing names for variables (unique identifiers) are:Names can contain letters, digits, underscores, and dollar signsNames must begin with a letterNames should start with a lowercase letter and it cannot contain whitespaceNames can also begin with $ and _ (but we will not use it in this tutorial)Names are case sensitive ("myVar" and "myvar" are different variables)Reserved words (like Java keywords, such as int or String) cannot be used as namesTest Yourself With ExercisesExercise:Create a variable named carName and assign the value Volvo to it. = ;

Java Data Types

Java Data TypesAs explained in the previous chapter, a variable in Java must be a specified data type:Exampleint myNum = 5; // Integer (whole number) float myFloatNum = 5.99f; // Floating point number char myLetter = 'D'; // Character boolean myBool = true; // Boolean String myText = "Hello"; // String Data types are divided into two groups:Primitive data types - includes byte, short, int, long, float,double, boolean and charNon-primitive data types - such as String, Arrays and Classes (you will learn more about these in a later chapter)Primitive Data TypesA primitive data type specifies the size and type of variable values, and it has no additional methods.There are eight primitive data types in Java:Data TypeSizeDescriptionbyte1 byteStores whole numbers from -128 to 127short2 bytesStores whole numbers from -32,768 to 32,767int4 bytesStores whole numbers from -2,147,483,648 to 2,147,483,647long8 bytesStores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807float4 bytesStores fractional numbers. Sufficient for storing 6 to 7 decimal digitsdouble8 bytesStores fractional numbers. Sufficient for storing 15 decimal digitsboolean1 bitStores true or false valueschar2 bytesStores a single character/letter or ASCII values

Numbers in java

Numbers in javaPrimitive number types are divided into two groups:Integer types stores whole numbers, positive or negative (such as 123 or -456), without decimals. Valid types are byte, short, int and long. Which type you should use, depends on the numeric value.Floating point types represents numbers with a fractional part, containing one or more decimals. There are two types: float and double.Even though there are many numeric types in Java, the most used for numbers are int (for whole numbers) and double (for floating point numbers). However, we will describe them all as you continue to read.

Integer Types

Integer TypesByteThe byte data type can store whole numbers from -128 to 127. This can be used instead of int or other integer types to save memory when you are certain that the value will be within -128 and 127:Examplebyte myNum = 100; System.out.println(myNum); ShortThe short data type can store whole numbers from -32768 to 32767:Exampleshort myNum = 5000; System.out.println(myNum); IntThe int data type can store whole numbers from -2147483648 to 2147483647. In general, and in our tutorial, the int data type is the preferred data type when we create variables with a numeric value.Exampleint myNum = 100000; System.out.println(myNum); LongThe long data type can store whole numbers from -9223372036854775808 to 9223372036854775807. This is used when int is not large enough to store the value. Note that you should end the value with an "L":Examplelong myNum = 15000000000L; System.out.println(myNum);

Floating Point Types

Floating Point TypesYou should use a floating point type whenever you need a number with a decimal, such as 9.99 or 3.14515.FloatThe float data type can store fractional numbers from 3.4e−038 to 3.4e+038. Note that you should end the value with an "f":Examplefloat myNum = 5.75f; System.out.println(myNum); DoubleThe double data type can store fractional numbers from 1.7e−308 to 1.7e+308. Note that you should end the value with a "d":Exampledouble myNum = 19.99d; System.out.println(myNum); Use float or double?The precision of a floating point value indicates how many digits the value can have after the decimal point. The precision of float is only six or seven decimal digits, while double variables have a precision of about 15 digits. Therefore it is safer to use double for most calculations.

Scientific Numbers in java

Scientific Numbers in javaA floating point number can also be a scientific number with an "e" to indicate the power of 10:Examplefloat f1 = 35e3f; double d1 = 12E4d; System.out.println(f1); System.out.println(d1); BooleansA boolean data type is declared with the boolean keyword and can only take the values true or false:Exampleboolean isJavaFun = true; boolean isFishTasty = false; System.out.println(isJavaFun); // Outputs true System.out.println(isFishTasty); // Outputs false Boolean values are mostly used for conditional testing, which you will learn more about in a later chapter.CharactersThe char data type is used to store a single character. The character must be surrounded by single quotes, like 'A' or 'c':Examplechar myGrade = 'B'; System.out.println(myGrade); Alternatively, you can use ASCII values to display certain characters:Examplechar a = 65, b = 66, c = 67; System.out.println(a); System.out.println(b); System.out.println(c); Tip: A list of all ASCII values can be found in our ASCII Table Reference.StringsThe String data type is used to store a sequence of characters (text). String values must be surrounded by double quotes:ExampleString greeting = "Hello World"; System.out.println(greeting); The String type is so much used and integrated in Java, that some call it "the special ninth type".A String in Java is actually a non-primitive data type, because it refers to an object. The String object has methods that is used to perform certain operations on strings. Don't worry if you don't understand the term "object" just yet. We will learn more about strings and objects in a later chapter.Non-Primitive Data TypesNon-primitive data types are called reference types because they refer to objects.The main difference between primitive and non-primitive data types are:Primitive types are predefined (already defined) in Java. Non-primitive types are created by the programmer and is not defined by Java (except forString).Non-primitive types can be used to call methods to perform certain operations, while primitive types cannot.A primitive type has always a value, while non-primitive types can be null.A primitive type starts with a lowercase letter, while non-primitive types starts with an uppercase letter.The size of a primitive type depends on the data type, while non-primitive types have all the same size.Examples of non-primitive types are Strings, Arrays, Classes, Interface, etc. You will learn more about these in a later chapter.Test Yourself With ExercisesExercise:Add the correct data type for the following variables: myNum = 9; myFloatNum = 8.99f; myLetter = 'A'; myBool = false; myText = "Hello World";

Java: maze code

This code creates a game called maze in the programming language of java. use either netbeans . intelli j or eclipse to execute this file.

Main features of java

The main features of java are simple, robust, and connectivity etc. ... They explain how easy the Java programming language is.

JavaScript Exercises

JavaScript ExercisesTest Yourself With ExercisesExercise:Create a variable called carName and assign the value Volvo to it.var = ""; Submit Answer »Start the ExerciseJavaScript Quiz TestTest your JavaScript skills at W3Schools!Start JavaScript Quiz!JavaScript Exam - Get Your Diploma!W3Schools' Online CertificationThe perfect solution for professionals who need to balance work, family, and career building.More than 25 000 certificates already issued!The HTML Certificate documents your knowledge of HTML.The CSS Certificate documents your knowledge of advanced CSS.The JavaScript Certificate documents your knowledge of JavaScript and HTML DOM.The Python Certificate documents your knowledge of Python.The jQuery Certificate documents your knowledge of jQuery.The SQL Certificate documents your knowledge of SQL.The PHP Certificate documents your knowledge of PHP and MySQL.The XML Certificate documents your knowledge of XML, XML DOM and XSLT.The Bootstrap Certificate documents your knowledge of the Bootstrap framework.

JavaScript Introduction

JavaScript Can Change HTML ContentOne of many JavaScript HTML methods is getElementById().This example uses the method to "find" an HTML element (with id="demo") and changes the element content (innerHTML) to "Hello JavaScript":Exampledocument.getElementById("demo").innerHTML = "Hello JavaScript";JavaScript accepts both double and single quotes:Exampledocument.getElementById('demo').innerHTML = 'Hello JavaScript';