KEMBAR78
Java Basics 1.pptx
Java basics 1
Java Introduction
What is Java?
 Java is a popular programming language, created in
1995.
 It is owned by Oracle, and more than 3 billion
devices run Java.
 It is used for:
 Mobile applications (specially Android apps)
 Desktop applications
 Web applications
 Web servers and application servers
 Games
 Database connection
 And much, much more!
Why Use Java?
 Java works on different platforms (Windows, Mac,
Linux, Raspberry Pi, etc.)
 It is one of the most popular programming language in
the world
 It is easy to learn and simple to use
 It is open-source and free
 It is secure, fast and powerful
 It has a huge community support (tens of millions of
developers)
 Java is an object oriented language which gives a
clear structure to programs and allows code to be
reused, lowering development costs
 As Java is close to C++ and C#, it makes it easy for
programmers to switch to Java or vice versa
Java Install
 Some PCs might have Java already installed.
 To check if you have Java installed on a Windows
PC, search in the start bar for Java or type the
following in Command Prompt (cmd.exe):
 C:UsersYour Name>java -version
 In Java, every application begins with a class
name, and that class must match the filename.
 Let's create our first Java file, called
MyClass.java, which can be done in any text
editor (like Notepad).
 The file should contain a "Hello World" message,
which is written with the following code:
Hello World
 MyClass.java
 public class MyClass
{
public static void main(String[] args)
{
System.out.println("Hello World");
} }
 Don't worry if you don't understand the code
above - we will discuss it in detail in later
chapters. For now, focus on how to run the code
above.
 Save the code in Notepad as "MyClass.java".
Open Command Prompt (cmd.exe), navigate to
the directory where you saved your file, and type
"javac MyClass.java":
 C:UsersYour Name>javac MyClass.java
 This will compile your code. If there are no errors
in the code, the command prompt will take you to
the next line. Now, type "java MyClass" to run the
file:
 C:UsersYour Name>java MyClass
 Every line of code that runs in Java must be
inside a class. In our example, we named the
class MyClass. A class should always start with
an uppercase first letter.
 Note: Java is case-sensitive: "MyClass" and
"myclass" has different meaning.
 The name of the java file must match the class
name. When saving the file, save it using the
class name and add ".java" to the end of the
filename.
 The main Method
 The main() method is required and you will see it in
every Java program:
 public static void main(String[] args)
 Note: (in android we use oncreate() method where we
start our application)
 Any code inside the main() method will be executed.
You don't have to understand the keywords before
and after main. You will get to know them bit by bit
while reading this tutorial.
 For now, just remember that every Java program has
a class name which must match the filename, and
that every program must contain the main() method.
 System.out.println()
 Inside the main() method, we can use the println()
method to print a line of text to the screen:
 public static void main(String[] args) {
System.out.println("Hello World"); }
 Note: The curly braces {} marks the beginning
and the end of a block of code.
 Note: Each code statement must end with a
semicolon.
Java Comments
 Comments 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 (//).
 Multi-line comments start with /* and ends with */.
 Any text between /* and */ will be ignored by
Java.
Java Variables
 Variables 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 quotes
 int - stores integers (whole numbers), without
decimals, such as 123 or -123
 float - stores floating point numbers, with
decimals, such as 19.99 or -19.99
 char - stores single characters, such as 'a' or 'B'.
Char values are surrounded by single quotes
 boolean - stores values with two states: true or
false
Declaring (Creating) Variables
 To create a variable, you must specify the type and assign it a
value:
 Syntax
 type 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:
 Example
 Create 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:
 Example
 Create a variable called myNum of type int and assign it the
value 15:
 You can also declare a variable without assigning the value, and assign
the value later:
 Example
 int myNum; myNum = 15; System.out.println(myNum);
 Note that if you assign a new value to an existing variable, it will
overwrite the previous value:
 Example
 Change the value of myNum from 15 to 20:
 int myNum = 15; myNum = 20; // myNum is now 20
System.out.println(myNum);
 Final Variables
 However, you can add the final keyword if you don't want others (or
yourself) to overwrite existing values (this will declare the variable as
"final" or "constant", which means unchangeable and read-only):
 Example
 final int myNum = 15; myNum = 20; // will generate an error: cannot
assign a value to a final variable
Other Types
 A demonstration of how to declare variables of
other types:
 Example
 int myNum = 5;
 float myFloatNum = 5.99f;
 char myLetter = 'D';
 boolean myBool = true;
 String myText = "Hello";
Primitive and Non Primitive
 Data types are divided into two groups:
 Primitive data types - includes byte, short, int,
long, float, double, boolean and char
 Non-primitive data types - such as String, Arrays
and Classes (you will learn more about these in a
later chapter)
Java Type Casting
 Type casting is when you assign a value of one
primitive data type to another type.
 In Java, there are two types of casting:
 Widening Casting (automatically) - converting a
smaller type to a larger type size
byte -> short -> char -> int -> long -> float ->
double
 Narrowing Casting (manually) - converting a
larger type to a smaller size type
double -> float -> long -> int -> char -> short ->
byte
Widening Casting
 Widening casting is done automatically when
passing a smaller size type to a larger size type:
 Example
 public class MyClass { public static void
main(String[] args) { int myInt = 9; double
myDouble = myInt; // Automatic casting: int to
double System.out.println(myInt); // Outputs 9
System.out.println(myDouble); // Outputs 9.0 } }
Narrowing Casting
 Narrowing casting must be done manually by
placing the type in parentheses in front of the
value:
 Example
 public class MyClass { public static void
main(String[] args) { double myDouble = 9.78; int
myInt = (int) myDouble; // Manual casting: double
to int System.out.println(myDouble); // Outputs
9.78 System.out.println(myInt); // Outputs 9 } }
Java Operators
 Operators are used to perform operations on
variables and values.
 In the example below, we use the + operator to add
together two values:
 Example
 int x = 100 + 50;
 Java Operators
 Operators are used to perform operations on
variables and values.
 In the example below, we use the + operator to add
together two values:
 Example
 int x = 100 + 50;
Java Assignment Operators
 Assignment operators are used to assign values
to variables.
 In the example below, we use the assignment
operator (=) to assign the value 10 to a variable
called x:
 Example
 int x = 10;
 The addition assignment operator (+=) adds a
value to a variable:
 Example
 int x = 10; x += 5;
Java Comparison Operators
Java Logical Operators
Java Strings
 Strings are used for storing text.
 A String variable contains a collection of
characters surrounded by double quotes:
 Example
 Create a variable of type String and assign it a
value:
 String greeting = "Hello";
String Length
 A String in Java is actually an object, which
contain methods that can perform certain
operations on strings. For example, the length of
a string can be found with the length() method:
 Example
 String txt =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
System.out.println("The length of the txt string is:
" + txt.length());
More String Methods
 There are many string methods available, for
example toUpperCase() and toLowerCase():
 Example
 String txt = "Hello World";
System.out.println(txt.toUpperCase()); // Outputs
"HELLO WORLD"
System.out.println(txt.toLowerCase()); // Outputs
"hello world"
Finding a Character in a String
 The indexOf() method returns the index (the
position) of the first occurrence of a specified text
in a string (including whitespace):
 Example
 String txt = "Please locate where 'locate' occurs!";
System.out.println(txt.indexOf("locate")); //
Outputs 7
String Concatenation
 The + operator can be used between strings to
combine them. This is called concatenation:
 Example
 String firstName = "John"; String lastName =
"Doe"; System.out.println(firstName + " " +
lastName);
 You can also use the concat() method to
concatenate two strings:
 Example
 String firstName = "John "; String lastName =
"Doe";
System.out.println(firstName.concat(lastName))
 Special Characters
 Because strings must be written within quotes, Java
will misunderstand this string, and generate an error:
 String txt = "We are the so-called "Vikings" from the
north.";
 The solution to avoid this problem, is to use the
backslash escape character.
 The backslash () escape character turns special
characters into string characters:
 String txt = "We are the so-called "Vikings" from the
north.";
 For more visit
https://www.w3schools.com/java/java_strings.asp
Java Basics 1.pptx

Java Basics 1.pptx

  • 1.
    Java basics 1 JavaIntroduction
  • 2.
    What is Java? Java is a popular programming language, created in 1995.  It is owned by Oracle, and more than 3 billion devices run Java.  It is used for:  Mobile applications (specially Android apps)  Desktop applications  Web applications  Web servers and application servers  Games  Database connection  And much, much more!
  • 3.
    Why Use Java? Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.)  It is one of the most popular programming language in the world  It is easy to learn and simple to use  It is open-source and free  It is secure, fast and powerful  It has a huge community support (tens of millions of developers)  Java is an object oriented language which gives a clear structure to programs and allows code to be reused, lowering development costs  As Java is close to C++ and C#, it makes it easy for programmers to switch to Java or vice versa
  • 4.
    Java Install  SomePCs might have Java already installed.  To check if you have Java installed on a Windows PC, search in the start bar for Java or type the following in Command Prompt (cmd.exe):  C:UsersYour Name>java -version
  • 5.
     In Java,every application begins with a class name, and that class must match the filename.  Let's create our first Java file, called MyClass.java, which can be done in any text editor (like Notepad).  The file should contain a "Hello World" message, which is written with the following code:
  • 6.
    Hello World  MyClass.java public class MyClass { public static void main(String[] args) { System.out.println("Hello World"); } }
  • 7.
     Don't worryif you don't understand the code above - we will discuss it in detail in later chapters. For now, focus on how to run the code above.  Save the code in Notepad as "MyClass.java". Open Command Prompt (cmd.exe), navigate to the directory where you saved your file, and type "javac MyClass.java":
  • 8.
     C:UsersYour Name>javacMyClass.java  This will compile your code. If there are no errors in the code, the command prompt will take you to the next line. Now, type "java MyClass" to run the file:  C:UsersYour Name>java MyClass
  • 9.
     Every lineof code that runs in Java must be inside a class. In our example, we named the class MyClass. A class should always start with an uppercase first letter.  Note: Java is case-sensitive: "MyClass" and "myclass" has different meaning.  The name of the java file must match the class name. When saving the file, save it using the class name and add ".java" to the end of the filename.
  • 10.
     The mainMethod  The main() method is required and you will see it in every Java program:  public static void main(String[] args)  Note: (in android we use oncreate() method where we start our application)  Any code inside the main() method will be executed. You don't have to understand the keywords before and after main. You will get to know them bit by bit while reading this tutorial.  For now, just remember that every Java program has a class name which must match the filename, and that every program must contain the main() method.
  • 11.
     System.out.println()  Insidethe main() method, we can use the println() method to print a line of text to the screen:  public static void main(String[] args) { System.out.println("Hello World"); }  Note: The curly braces {} marks the beginning and the end of a block of code.  Note: Each code statement must end with a semicolon.
  • 12.
    Java Comments  Commentscan 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 (//).  Multi-line comments start with /* and ends with */.  Any text between /* and */ will be ignored by Java.
  • 13.
    Java Variables  Variablesare 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 quotes  int - stores integers (whole numbers), without decimals, such as 123 or -123  float - stores floating point numbers, with decimals, such as 19.99 or -19.99  char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes  boolean - stores values with two states: true or false
  • 14.
    Declaring (Creating) Variables To create a variable, you must specify the type and assign it a value:  Syntax  type 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:  Example  Create 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:  Example  Create a variable called myNum of type int and assign it the value 15:
  • 15.
     You canalso declare a variable without assigning the value, and assign the value later:  Example  int myNum; myNum = 15; System.out.println(myNum);  Note that if you assign a new value to an existing variable, it will overwrite the previous value:  Example  Change the value of myNum from 15 to 20:  int myNum = 15; myNum = 20; // myNum is now 20 System.out.println(myNum);  Final Variables  However, you can add the final keyword if you don't want others (or yourself) to overwrite existing values (this will declare the variable as "final" or "constant", which means unchangeable and read-only):  Example  final int myNum = 15; myNum = 20; // will generate an error: cannot assign a value to a final variable
  • 16.
    Other Types  Ademonstration of how to declare variables of other types:  Example  int myNum = 5;  float myFloatNum = 5.99f;  char myLetter = 'D';  boolean myBool = true;  String myText = "Hello";
  • 17.
    Primitive and NonPrimitive  Data types are divided into two groups:  Primitive data types - includes byte, short, int, long, float, double, boolean and char  Non-primitive data types - such as String, Arrays and Classes (you will learn more about these in a later chapter)
  • 18.
    Java Type Casting Type casting is when you assign a value of one primitive data type to another type.  In Java, there are two types of casting:  Widening Casting (automatically) - converting a smaller type to a larger type size byte -> short -> char -> int -> long -> float -> double  Narrowing Casting (manually) - converting a larger type to a smaller size type double -> float -> long -> int -> char -> short -> byte
  • 19.
    Widening Casting  Wideningcasting is done automatically when passing a smaller size type to a larger size type:  Example  public class MyClass { public static void main(String[] args) { int myInt = 9; double myDouble = myInt; // Automatic casting: int to double System.out.println(myInt); // Outputs 9 System.out.println(myDouble); // Outputs 9.0 } }
  • 20.
    Narrowing Casting  Narrowingcasting must be done manually by placing the type in parentheses in front of the value:  Example  public class MyClass { public static void main(String[] args) { double myDouble = 9.78; int myInt = (int) myDouble; // Manual casting: double to int System.out.println(myDouble); // Outputs 9.78 System.out.println(myInt); // Outputs 9 } }
  • 21.
    Java Operators  Operatorsare used to perform operations on variables and values.  In the example below, we use the + operator to add together two values:  Example  int x = 100 + 50;  Java Operators  Operators are used to perform operations on variables and values.  In the example below, we use the + operator to add together two values:  Example  int x = 100 + 50;
  • 23.
    Java Assignment Operators Assignment operators are used to assign values to variables.  In the example below, we use the assignment operator (=) to assign the value 10 to a variable called x:  Example  int x = 10;  The addition assignment operator (+=) adds a value to a variable:  Example  int x = 10; x += 5;
  • 25.
  • 26.
  • 27.
    Java Strings  Stringsare used for storing text.  A String variable contains a collection of characters surrounded by double quotes:  Example  Create a variable of type String and assign it a value:  String greeting = "Hello";
  • 28.
    String Length  AString in Java is actually an object, which contain methods that can perform certain operations on strings. For example, the length of a string can be found with the length() method:  Example  String txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; System.out.println("The length of the txt string is: " + txt.length());
  • 29.
    More String Methods There are many string methods available, for example toUpperCase() and toLowerCase():  Example  String txt = "Hello World"; System.out.println(txt.toUpperCase()); // Outputs "HELLO WORLD" System.out.println(txt.toLowerCase()); // Outputs "hello world"
  • 30.
    Finding a Characterin a String  The indexOf() method returns the index (the position) of the first occurrence of a specified text in a string (including whitespace):  Example  String txt = "Please locate where 'locate' occurs!"; System.out.println(txt.indexOf("locate")); // Outputs 7
  • 31.
    String Concatenation  The+ operator can be used between strings to combine them. This is called concatenation:  Example  String firstName = "John"; String lastName = "Doe"; System.out.println(firstName + " " + lastName);  You can also use the concat() method to concatenate two strings:  Example  String firstName = "John "; String lastName = "Doe"; System.out.println(firstName.concat(lastName))
  • 32.
     Special Characters Because strings must be written within quotes, Java will misunderstand this string, and generate an error:  String txt = "We are the so-called "Vikings" from the north.";  The solution to avoid this problem, is to use the backslash escape character.  The backslash () escape character turns special characters into string characters:  String txt = "We are the so-called "Vikings" from the north.";  For more visit https://www.w3schools.com/java/java_strings.asp