Monday, March 21, 2022

Java identifers

 

Identifiers

All Java variables must be identified with unique names.

For example -different people have different names

These unique names are called identifiers.

Identifiers can be short names (like a and b) or more specific names (program,code ,techySowmi).

Note: It is suggested to use specific names in order to create understandable and maintainable code:

example

public class Main {

public static void main(String[] args) {

// Good

String howAreYou= “fine”;

int totalMarks = 90;

// OK, but not so easy to understand what a ,b actually is

String a = “fine”;

int b=90;

System.out.println(howAreYou);

System.out.println(a);

System.out.println(b);

System.out.println(totalMarks);

}

}

Output-

fine

fine

90

90



The general rules for naming variables are:

  • Names can contain letters, digits, underscores, and dollar signs

  • Names must begin with a letter

  • Names should start with a lowercase letter and it cannot contain whitespace

  • Names 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 boolean) cannot be used as names



Saturday, March 19, 2022

Java Declare Multiple Variables

 

Declare Many Variables

To declare more than one variable of the same type, you can use a comma-separated list:

Example

Instead of writing:

int a =5;

int b= 6;

int c=7;

System.out.println(a+b+c);

You can simply write:

int a =5, b= 6,c=7;

System.out.println(a+b+c);

Example

public class Main {

  public static void main(String[] args) {

    int a=5,b=6,c=7;

    System.out.println(a+b+c);

  }

}

One Value to Multiple Variables

You can also assign the same value to multiple variables in one line:

Example

int a, b, c;

a= b = c = 25;

System.out.println(a + b + c);


Java Print Variables

 

Display Variables

The println() method is recurrently used to display variables.

To combine both text and a variable, use the + character:

Example

public class Main {

public static void main(String[] args) {

String name = "Sowmii";

 System.out.println("Hello " + name);

}
}

  Output=Hello Sowmii


You can also use the + character to add a variable to another variable:

Example

public class Main {
  public static void main(String[] args) {
    String name1 = "Welcome";
    String name2 = "to techy sowmii";
    String name = name1 + name2;
    System.out.println(name); 
  }
}

Output= Welcome to techy sowmii

For numbers, the + character works as a mathematical operator

Example


public class Main {
  public static void main(String[] args) {
    int g = 5;
    int q = 6;
    System.out.println(g + q); // Print the value of g + q
  }
}
Output=11

From the above example, you can sense that

  • g stores the value 5
  • q stores the value 6
  • Then we use the println method to display the value of g+ q, which is 11

JAVA VARIABLES

 

Java Variables

Variables are containers for storing data values.

In Java, there are diverse varieties 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  Variables-

To generate a variable, you should identify the type and reserve it a value:

Syntax-

type variableName = value;

  • From the above line of code type describes java variables type for example int or String furthermore variableName is the name of the variable ... for example- name or sowmii etcc....

  • the equal to sign on the right hand side shows the value reserved for the variable.......

  • for example String name = "sowmiii"; 

  • String= variable type

  • name=variable name

  • sowmii=value of the variable

To create a variable that should store text, look at the following example:

Example -1

Create a variable called text of type  of variable is String and assign it the value “Java”..

public class Main {
public static void main(String[] args) {
String name = "Java";
System.out.println(name);
}
}

Output-Java

To create a variable that should store a number, look at the following example:

Example-2

Create a variable called number of type int and assign it the value 45-

public class Main {

public static void main(String[] args) {

int number= 45;

System.out.println(number);

}

}

15 Output-45

You can also announce or declare a variable without assigning the value and assign the value the later.......

Example

public class Main {

public static void main(String[] args) {

int number;

number= 45;

System.out.println(number);

}

}

Output-45

Note that if you allocate a new value to an existing variable, it will overwrite the previous value:

Example

Change the value of number from 45 to 80:

public class Main {

public static void main(String[] args) {

int number = 45;

number = 80; // number is now 80

System.out.println(number);

}

}

Output-80

Final Variables

If you don't want anyone to overwrite existing values, use the final keyword (this will announce the variable as "final" or "constant", which means permanent and read-only):

Example

public class Main {

public static void main(String[] args) {

final int number= 15;

number= 20; // will generate an error

System.out.println(number);

}

}

output: Main.java :4: error: cannot assign a value to final variable  number

number =20;

^

1 error

Other Types

A trail of how to declare variables of other types:

Example

int number= 5;
float number = 5.99f;
char number = 'D';
boolean bool= true;
String text = "Hello";

Related articles

Beginner guide for java -1

why java

Explore JDK,JVM,JRE versions

A simple pillow talk between java and java user!!!!!!!!........

write java code in a simple text editor

java setup path for windows

java syntax-1

Java Syntax Explanation

Java Declare Multiple Variables

Java Print Variables

JAVA VARIABLES

Comments in Java







Thursday, March 17, 2022

Comments in Java

 

java comments-

  • comments illustrate  java code and it makes the code more smooth and lucid
  • lines of code inside the comments are isolated by  the compiler in time of the compiling process......
  • Single-line Comments
  • Single-line comments start with two forward slashes (//).

Anything between // and the end of the line is ignored by Java compiler (will not be executed).

This example uses a single-line comment before a line of code:


Example

  1.public class Best{

  public static void main(String[] args) {

   // This is a comment

      System.out.println("Welcome to techy sowmii");

     }

     }

output: Welcome to techy sowmii

comments are not executed by compiler

 2.public class Best{

  public static void main(String[] args) {

   // This is a comment

      System.out.println("Welcome to techy sowmii");   // This is a comment

     }

     }

output: Welcome to techy sowmii

you can use java comments all over between the lines of code

Java Multi-line Comments

  • Multi-line comments start with /* and ends with */.
  • Anything between /* and */ will be ignored by Java compiler in time of compiling and execution process
  • Example

public class Best{

/* The code below will print the words Hello World

to the screen, and it is incredible*/

  public static void main(String[] args) {

      System.out.println("Welcome to techy sowmii");  

     }

     }

Normally, we use // for short comments

 /* */ for longer comments.

Related articles

JAVA VARIABLES

Beginner guide for java -1

why java

Explore JDK,JVM,JRE versions

A simple pillow talk between java and java user!!!!!!!!........

write java code in a simple text editor

java setup path for windows

java syntax-1

Java Syntax Explanation

Java Declare Multiple Variables

Java Print Variables

Comments in Java

Java Syntax Explanation

 Java syntax explanation 

      public class Best{

      public static void main(String[] args) {

      System.out.println("Welcome to techy sowmii");

     }

     }

 Starting with Class

public class Best{

  • every program must start with a class definition and here we defined the class as Best and file name must and should match with the class name when saving the file, save it using the class name and add ".java" to the end of the filename .
  • java is case sensitive MyClass and my class has different meanings. the class has a naming convention and in the class name the first letter starts with an uppercase letter then followed by lowercase
  • examples of class are Best, MyClass, JavaCode, etc
  • examples of file name are Best.java,MyClass.java,JavaCode.java

Method

  • every Java program has a class name that should match the filename, and every program must contain the main() method.
  •  the code inside this main method gets compiled and then executed by the computer
  • Example -public static void main(String[] args) {

System.out.println()

Inside the main() method, we can use the println() method to print a line of text to the screen:

System. out.println("Welcome to techy sowmii");

These println method prints Welcome to techy sowmii to the screen

The curly braces {} marks the beginning and the end of a block of code.

Each code statement must end with a semicolon(;)...........

Related articles

Beginner guide for java -1

why java

Explore JDK,JVM,JRE versions

A simple pillow talk between java and java user!!!!!!!!........

write java code in a simple text editor

java setup path for windows

java syntax-1

Java Syntax Explanation

Java Declare Multiple Variables

Java Print Variables

JAVA VARIABLES

Comments in Java


Wednesday, March 16, 2022

java syntax-1

 

Lets start writing code in java

In Java, every application begins with a class name, and that class name.must match the filename.

lets save the file name as Best.java

this file should print the output welcome to techy sowmii

Best.java (java FILE)

public class Best{

public static void main(String[] args) {

System.out.println("Welcome to techy sowmii");

}

}

Output:Welcome to techy sowmii

you can run the java with the help of simple text editor like notepad and java software must present in the system .

Save the code in Notepad as "Main.java". Open Command Prompt (cmd.exe), navigate to the directory where you saved your file, and type "javac Main.java"

C:\Users\Your Name>javac Main.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 Main" to run the file:

C:\Users\Your Name>java Main

The output should read:

Hello World

RELATED ARTICLES


java setup path for windows

 

Environment variables:

The environment variables store data that is used by the operating system, other programs, and user data. For example, the WINDIR environment variable contains the location of the Windows installation directory. Programs can query the value of this variable to determine where Windows operating system files are located.

Setup for Windows

To install Java on Windows:

STEP1

Go to "System Properties" (Can be found on Control Panel > System and Security > System > Advanced System Settings) or 



STEP2

Click on the "Environment variables" button under the "Advanced" tab



STEP3

Then, select the "Path" variable in System variables and click on the "Edit" button

Click on the "New" button and add the path where Java is installed, followed by \bin. By default, Java is installed in C:\Program Files\Java\jdk-11.0.1 (If you have made some changes during the installation process). In that case, You will have to add a new path with C:\Program Files\Java\jdk-11.0.1\bin.

Then, click "OK", and save the settings.



STEP4


write the below commands in command prompt


At last, open Command Prompt (cmd.exe) and type java -version to see if Java is running on your machine

Write the following in the command line (cmd.exe):

C:\Users\Your Name>java -version

If Java was successfully installed, you will see something like this (depending on the version):

java version "11.0.1" 2018-10-16 LTS

Java(TM) SE Runtime Environment 18.9 (build 11.0.1+13-LTS)

Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.1+13-LTS, mixed mode)

RELATED ARTICLES


Java identifers

  Identifiers All Java variables must be identified with unique names . For example -different people have different names These unique ...