Posts

Showing posts from June, 2020

Naming variables

Why is naming important? As you may know, every variable has a name that uniquely identifies it among other variables. Giving a good name to a variable may not be as simple as it seems. Experienced programmers put a lot of care into naming to make their programs easy to understand. It is important because programmers spend much time on reading and understanding code written by other programmers. If variables have bad names, even your own code will seem unclear to you in a few months. Always try to give descriptive and concise names to all variables. As a result, any programmer will enjoy your code for a long time. In addition, there are two sets of rules that restrict the possible names for variables. Rules for naming variables Java has some rules for naming variables: ⏭ Names are case-sensitive; ⏭ A name can include Unicode letters, digits, and two special characters ($, _); ⏭ A name cannot start with a digit; ⏭ A name must not be a keyword (class, static, int are illegal names). Base...

Comments

Inside a Java program, you can write a special text that will be ignored by the java compiler — known as the comment. Comments allow you to exclude code from the compilation process (disable it) or clarify a piece of code to yourself or other developers. In our materials, we use comments in the theory and practice lessons to explain how and why our code works. The Java programming language supports three kinds of comments. End-of-line comments The java compiler ignores any text from // to the end of the line. class Program {     public static void main(String[] args) {         // The line below will be ignored         // System.out.println("Hello, World");         // It prints the string "Hello, Java"         System.out.println("Hello, Java"); // Here can be any comment     } } In the example above the text after // is ignored by the compiler. Multi-line comments The compiler ignores any ...

Accessing the value of a variable

Once a variable has been declared, its value can be accessed and modified using the name. In the example below, we declare a variable and then print it: String dayOfWeek = "Monday"; System.out.println(dayOfWeek); // Monday It is also possible to assign a value of one variable to another one: int one = 1; int num = one; System.out.println(num); // 1 One important feature of variables is that they can be changed. You don't need to declare a variable again to change its value; just assign a new value to it using the = operator. Let's declare a variable named dayOfWeek and print its value before and after changing: String dayOfWeek = "Monday"; System.out.println(dayOfWeek); // Monday   dayOfWeek = "Tuesday"; System.out.println(dayOfWeek); // Tuesday There is one restriction for variables: you can only assign a value of the same type as the type of the initial variable. So, the following code is not correct: int number = 10; number = 11; // ok number = ...

Types and variables

Declaring and initializing A variable is a placeholder for storing a value of a particular type: a string, a number, or something else. Every variable has a name (also known as an identifier) to distinguish it from others. Before we start using a variable, we must declare it. The general form of declaration is the following: DataType variableName = initialization; The left part of this statement describes the variable, and the right part describes something that is assigned to it. The type (or data type) of a variable determines what possible operations can be performed on the variable and which values can be stored in it. Here we use a non-existing data type (DataType) to demonstrate the general form of declaration. The name (or identifier) distinguishes the variable from others. The name of a variable cannot start with a digit; it usually starts with a letter. Always try to choose meaningful and readable names for variables to make your code easy to understand. The assignment operato...

Displaying text using println() and print()

Standard output is a receiver to which a program can send information (text). It is supported by all common operating systems. Java provides a special object System.out to work with the standard output. We will often use it to print something. The println method displays the passed string followed by a new line on the screen (print-line). As an example, the following code fragment prints four lines. System.out.println("I "); System.out.println("know "); System.out.println("Java "); System.out.println("well."); The output: I know Java well. All strings were printed as is, without the double quotes. The method allows you to print the empty line when no string is given: System.out.println("Java is a popular programming language."); System.out.println(); // prints empty line System.out.println("It is used all over the world!"); The output: Java is a popular programming language.   It is used all over the world! The print method di...

Overview of the basic program

In this topic, we will build our very first Java program. Our program will simply print "Hello, World!" on the screen (a tradition by most programmers when learning new languages). Our code may not seem too exciting at first, however, we will learn about the basic template that all Java programs need to follow. The Hello World program public class Main {     public static void main(String[] args) {         System.out.println("Hello, World!");     } }  The result will be: Hello, World! The basic terminology Now that we have seen the result, let's learn some basic terminology and then try to understand this program. Program – a sequence of instructions (called statements), which are executed one after another in a predictable manner. Sequential flow is the most common and straightforward sequence of statements, in which statements are executed in the order that they are written – from top to bottom in a sequential manner; Statement – a single...
Literals Regardless of its complexity, a program always performs operations on numbers, strings, and other values. These values are called literals. There are many different sorts of literals in Java, but in this topic we will focus only on a few of them: the ones that surround us all the time in everyday life. Let's consider integer numbers, strings, and characters in the format in which they are written in Java. Integer numbers These numbers are used to count things in the real world. Also, we will often use them in Java. Here are several examples of valid integer number literals separated by commas: 0, 1, 2, 10, 11, 100. If an integer value contains a lot of digits, we can add underscores to divide the digit into blocks for increased readability: 1_000_000. It's more readable than the same value written as 1000000. Characters A single character can represent a digit, a letter or another symbol. To write a character we use single quotes as follows: 'A', 'B', ...

Introduction to java

Image
What is Java? Java is a general-purpose, modern programming language initially developed by Sun Microsystems, and currently owned by Oracle Corporation. The key feature of the language is platform independence: it means that the same Java program can be run on multiple platforms without any changes! This principle is also known as "write once, run anywhere" (or WORA). Java has been one of the most popular programming languages for many years. It has earned the top position in the TIOBE index (a programming language popularity index). This language is used by a huge community of developers around the world!  Java is used in our Android smartphones, the financial services industry, telecommunications, embedded systems, and in many other areas. Medical applications use it to store patient data, computer games such as Minecraft are created using Java; development tools like IntelliJ IDEA and Eclipse wouldn't exist without it. Brief History of JAVA The Java language project wa...

Plateform depenency and independency

One of the most important features of java is platform independency and to understand how java achieved platform indecency, one must learn what is platform dependency. Let us understand what is a platform to understand platform dependency. Platform is a combination of hardware and software. Hardware mainly refers to microprocessors and software refers to the Operating system. For example, the platform in your computer could be the combination of i5 processor and windowsOS or it could be i9 processor and mac OS as shown below. However, from the software engineers perspective, platform only refers to OS. Platform=Hardware + Software. After getting to know what is platform let us now understand platform dependency by considering different cases of C language which is very much platform-independent. we considered a file of C  programming language as the extension is  exmp.c.  Since  C  programming language is platform dependent programming language,  its platfo...

wondering how java program executes

Image
We’ll see how it executes but first let’s get to know how a C program executes. And then we'll compare it with how java program executes. From the above diagram, we can see that C executes faster than Java as it has fewer steps to execute. In java, we have an extra step that is Compiler converts the source file to byte code (This is where platform independency is achieved). Then JVM converts byte code to machine language and is fed to the processor to get output. Applications Applications are of two types: ➡️Embedded applications ➡️Portable applications Embedded applications: An embedded application is software that is placed permanently inside some kind of device to perform a very specific set of functions. The program instructions for embedded systems are called firmware, or embedded software, and are stored in read-only memory, or flash memory chips. For embedded applications Speed is a very important factor. Portable applications: A portable application(portable app), sometimes...

Introduction to microprocessor

Before you learn any programming language, it is important for one to understand some of the basics about computers and what are the languages that a  computer can understand. Let’s have a view on it. A computer is a collection of hardware components. Let us consider here a few hardware components such as  Microprocessor, RAM, Hard disk,  Motherboard                                                                                       Out of these and many other hardware components,   the most important or the heart of the computer is the Microprocessoror CPU . Microprocessor or  CPU: A microprocessor is an electronic component that is used by a   computer to do its work. It is a   central processing unit on a single inte...