COMPUTER-PROGRAMMING-1 | PDF | Data Type | Computer Programming
0% found this document useful (0 votes)
2 views26 pages

COMPUTER-PROGRAMMING-1

The document provides an overview of computer programming, explaining its definition, importance, and various programming languages. It categorizes programming languages into low-level, high-level, and middle-level, detailing their characteristics and examples such as Python, Java, and C++. Additionally, it outlines the responsibilities, skills, and job categories for programmers, emphasizing the significance of programming in various fields and its impact on automation and efficiency.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
2 views26 pages

COMPUTER-PROGRAMMING-1

The document provides an overview of computer programming, explaining its definition, importance, and various programming languages. It categorizes programming languages into low-level, high-level, and middle-level, detailing their characteristics and examples such as Python, Java, and C++. Additionally, it outlines the responsibilities, skills, and job categories for programmers, emphasizing the significance of programming in various fields and its impact on automation and efficiency.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 26

Republic of the Philippines

President Ramon Magsaysay State University


(Formerly Ramon Magsaysay Technological University)
Iba, Zambales, Philippines
Tel/Fax No.: (047) 811-1683
COMPUTER PROGRAMMING 1

BASIC PROGRAM STRUCTURE

I. Introduction to Computer Program

What is Computer Programming?

Computer programming is the process of designing and developing various computer programs to
complete specific tasks in either application or software or within the computers. The process
constitutes many tasks, like analyzing the problem, generating an algorithm to solve it, and then
examining the output generated. Computer programming is creating a series of programs to resolve
a problem in a computer.

The whole procedure of writing computer programs is called Computing Programming. The
programming also has another name called "coding" and the person who writes the code is
referred as the "Programmer" or "Coder". Programming is inputting instructions to the computer or
machines that describe how to carry out a task. Programmers use various programming languages to
code the different parts of the system, like software, hardware, and applications in computers and
digital devices, which helps us use them for many tasks. There are various programming languages
to write programs which include:

C, C++, Java, Python, PHP, JavaScript, Ruby, R, etc.

Why do we need programming?

The main goal of programming is to make tasks easier because if we program a task for one time, we
can reuse it in many similar situations, saving time. With the help of programming, only machines
can understand user-given commands. Computer programming will impact the future by creating
even more automated processes requiring less human interaction.

Banking from any location is possible only by programming. For example, consider an online banking
website or application which opens a user portal from which the applicants can make transactions
using a smartphone or computer; by this, we can save time, energy, and physical work of going to
the bank for every small need.
Apart from the advantage mentioned above, we can also make our carrier in programming, which is
a huge demand nowadays.

There are various groups of programming languages. Let us discuss some of them on this page,
described below.

Programming Language

As we know, to communicate with a person, we need a specific language, similarly to communicate


with computers, programmers also need a language is called Programming language.

Before learning the programming language, let's understand what is language?

What is Language?

Language is a mode of communication that is used to share ideas, opinions with each other. For
example, if we want to teach someone, we need a language that is understandable by both
communicators.

What is a Programming Language?

A programming language is a computer language that is used by programmers (developers) to


communicate with computers. It is a set of instructions written in any specific language ( C, C++,
Java, Python) to perform a specific task.

A programming language is mainly used to develop desktop applications, websites, and mobile
applications.

Types of programming language

1. Low-level programming language

Low-level language is machine-dependent (0s and 1s) programming language. The


processor runs low- level programs directly without the need of a compiler or interpreter, so the
programs written in low-level language can be run very fast.

Low-level language is further divided into two parts -

 Machine Language

Machine language is a type of low-level programming language. It is also called


as machine code or object code. Machine language is easier to read because it is normally
displayed in binary or hexadecimal form (base 16) form. It does not require a translator to
convert the programs because computers directly understand the machine language
programs.

The advantage of machine language is that it helps the programmer to execute the programs
faster than the high-level programming language.
 Assembly Language

Assembly language (ASM) is also a type of low-level programming language that is


designed for specific processors. It represents the set of instructions in a symbolic and
human-understandable form. It uses an assembler to convert the assembly language to
machine language.

The advantage of assembly language is that it requires less memory and less execution time
to execute a program.

2. High-level programming language

High-level programming language (HLL) is designed for developing user-friendly software


programs and websites. This programming language requires a compiler or interpreter to translate
the program into machine language (execute the program).

The main advantage of a high-level language is that it is easy to read, write, and maintain.

High-level programming language includes Python, Java, JavaScript, PHP, C#, C++, Objective
C, Cobol, Perl, Pascal, LISP, FORTRAN, and Swift programming language.

A high-level language is further divided into three parts -

 Procedural Oriented programming language

Procedural Oriented Programming (POP) language is derived from structured


programming and based upon the procedure call concept. It divides a program into small
procedures called routines or functions.

Procedural Oriented programming language is used by a software programmer to


create a program that can be accomplished by using a programming editor like IDE, Adobe
Dreamweaver, or Microsoft Visual Studio.

The advantage of POP language is that it helps programmers to easily track the program flow
and code can be reused in different parts of the program.

 Object-Oriented Programming language

Object-Oriented Programming (OOP) language is based upon the objects. In


this programming language, programs are divided into small parts called objects. It is used
to implement real-world entities like inheritance, polymorphism, abstraction, etc in the
program to makes the program resusable, efficient, and easy-to-use.

The main advantage of object-oriented programming is that OOP is faster and easier to
execute, maintain, modify, as well as debug.

Example: C++, Java, Python, C#, etc.


 Natural language

Natural language is a part of human languages such as English, Russian, German,


and Japanese. It is used by machines to understand, manipulate, and interpret human's
language. It is used by developers to perform tasks such as translation, automatic
summarization, Named Entity Recognition (NER), relationship extraction, and topic
segmentation.

The main advantage of natural language is that it helps users to ask questions in any subject
and directly respond within seconds.

3. Middle-level programming language

Middle-level programming language lies between the low-level programming language and
high-level programming language. It is also known as the intermediate programming language and
pseudo-language.

A middle-level programming language's advantages are that it supports the features of high-
level programming, it is a user-friendly language, and closely related to machine language and
human language.

Example: C, C++, language

Most commonly used Programming Language

As we all know, the programming language makes our life simpler. Currently, all sectors (like
education, hospitals, banks, automobiles, and more ) completely depend upon the programming
language.

There are dozens of programming languages used by the industries. Some most widely used
programming languages are given below -

1. Python

Python is one of the most widely used user-friendly programming languages. It is an open-
source and easy to learn programming language developed in the 1990s. It is mostly used
in Machine learning, Artificial intelligence, Big Data, GUI based desktop applications, and Robotics.

Advantages
o Python is easy to read, easy to understand, and easy to write.
o It integrates with other programming languages like C, C++, and Java.
o Python executes code line-by-line, so it is easy for the programmer to find the error that
occurred in the code.
o Python is platform-independent means you can write code once and run it anywhere.

Disadvantages

o Python is not suitable for developing mobile applications and games.


o Python works with the interpreter. That's why it is slower than other programming
languages like C and C++.

2. Java

Java is a simple, secure, platform-independent, reliable, architecture-neutral high-level


programming language developed by Sun Microsystems in 1995. Now, Java is owned by Oracle. It is
mainly used to develop bank, retail, information technology, android, big data, research community,
web, and desktop applications.

Advantages

o Java is easy to write, compile, learn, and debug as compared to other programming
languages.
o It provides an ability to run the same program on different platforms.
o It is a highly secured programming language because in java, there is no concept of explicit
pointers.
o It is capable of performing multiple tasks at the same time.

Disadvantages

o Java consumes more memory and slower than other programming languages like C or C++.
o It does not provide a backup facility.

3. C
C is a popular, simple, and flexible general-purpose computer programming language.
Dennis M Ritchie develops it in 1972 at AT&T. It is a combination of both low-level programming
language as well as a high-level programming language. It is used to design applications like Text
Editors, Compilers, Network devices, and many more.

Advantages

o C language is easy to learn.


o It is fast, efficient, portable, easy to extend, powerful, and flexible programming language.
o It is used to perform complex calculations and operations such as MATLAB.
o It provides dynamic memory allocation to allocate memory at the run time.

Disadvantages

o In the C programming language, it is very difficult to find the errors.


o C does not support the concepts of constructors, destructors, abstraction, polymorphism,
encapsulation, and namespace like OOPs.

4. C++

C++ is one of the thousands of programming languages that we use to develop software. C++
programming language is developed by Bjarne Stroustrup in 1980. It is similar to the C programming
language but also includes some additional features such as exception handling, object-oriented
programming, type checking, etc.

Advantages

o C++ is a simple and portable structured programming language.


o It supports OOPs features such as Abstraction, Inheritance, Encapsulation.
o It provides high-level abstraction and useful for a low-level programming language, and
more efficient for general-purpose.
o C++ is more compatible with the C language.
Disadvantages

o C++ programming language is not secured as compared to other programming languages like
Java or Python.
o C++ can not support garbage collection.
o It is difficult to debug large as well as complex web applications.

5. C#

C# (pronounced as C sharp) is a modern, general-purpose, and object-oriented programming


language used with XML based Web services on the .NET platform. It is mainly designed to improve
productivity in web applications. It is easier to learn for those users who have sufficient knowledge
of common programming languages like C, C++, or Java.

Advantages

o C# is a modern, type-safe, easy, fast, and open-source programming language that is easily
integrated with Windows.
o The maintenance of C# (C sharp) is lower than the C++ programming language.
o C# is a pure object-oriented programming language.
o C# includes a strong memory backup facility. That's why it avoids the problem of memory
leakage.

Disadvantages

o C# is less flexible because it is completely based on Microsoft .Net framework.


o In C#, it is difficult to write, understand, debug, and maintain multithreaded applications.

6. JavaScript

JavaScript is a type of scripting language that is used on both client-side as well as a server-
side. It is developed in the 1990s for the Netscape Navigator web browser. It allows programmers
to implement complex features to make web pages alive. It helps programmers to create dynamic
websites, servers, mobile applications, animated graphics, games, and more.

Advantage

o JavaScript helps us to add behavior and interactivity on the web page.


o It can be used to decrease the loading time from the server.
o It has the ability to create attractive, dynamic websites, and rich interfaces.
o JavaScript is a simple, versatile, and lightweight programming language.
o JavaScript and its syntax are easy to understand.

Disadvantage

o JavaScript is completely based on the browser.


o It does not support multiple inheritance.
o It is less secure compared to other programming languages.

7. PHP

PHP stands for Hypertext Preprocessor. It is an open-source, powerful server-side scripting


language mainly used to create static as well as dynamic websites. It is developed by Rasmus Laird
in 1994. Inside the php, we can also write HTML, CSS, and JavaScript code. To save php file, file
extension .php is used.

Advantages

o PHP is a more secure and easy-to-use programming language.


o It supports powerful online libraries.
o It can be run on a variety of operating systems such as Windows, Linux, and Mac.
o It provides excellent compatibility with cloud services.

Disadvantages

o PHP is not capable of handling a large number of applications and not suitable for large
applications.
o It is quite difficult to maintain.

Computer Programming and its uses

Computer programs solve many problems and create something innovative in various fields like
agriculture, education, entertainment, etc.
o Graphics are used in developing cartoons and adding realistic effects to movies using
programming languages.
o Computers are used to build various techniques in medical tests to detect diseases using
artificial intelligence and deep learning techniques.
o Used to develop mobile and android applications.
o Used Word and Excel to accomplish tasks with efficiency.
o Computer programming is also useful in business and marketing, where people in business
use applications and tools to interact with customers.
o Programming helps to carry out government functions where people can access services
more effectively, designing news portals to provide information about the government's
tasks and building websites to provide government services online.
o Programming helps in every point of our lives, from using smartphones to watching movies
online. Everything is possible because of coding itself.

Computer Programmers Responsibilities

o Computer Programmers, as said before, write code for the specific problem using various
programming languages like Java, C, python, JavaScript, and pearl. They also use scripting
languages like HTML and CSS based on their field of work.
o Consider a computer programmer developing a website, which will require all the
programming languages to develop the front-end, and backend.
o Programmers must be well versed in more than one programming language because as they
work in groups, one programmer should understand the code written by the other to
communicate the logic and code.
o Programmers must mingle and cooperate with software developers (to discuss ideas) and
business analysts (to adjust the finance) for outputting software design models.
o Programmers must be able to construct prototypes from the given constraints.
o They should be able to convert software models and instructions into a functioning code
with high efficiency.
o They should be capable of quickly debugging and modifying the code according to the
requirements.

The procedure of developing the program from scratch to deployment involves many steps, which
are described as follows:

o The programmer will first understand the problem statement and quickly builds an idea.
o The programmer will visualize the concepts he can apply to resolve a problem.
o Then they develop and writes the code, which is a preferable quality in a computer
programmer.
o The programmer then removes errors by debugging the code written.
o After debugging, he will test the code and application created before handing it over to the
users. Testing helps in reducing the bugs in code.
o The programmers will often encounter errors that can take a long time to remove. In such
situations also, they should have patience and dedication to solve the errors and bugs.

Skills and requirements for a Programmer

o Programmers should develop a quality of writing clean and well-organized code.


o The programmer should have problem-solving skills to solve real-world problems with
efficiency.
o Programmers must have good communication skills along with good interpersonal skills.
o The programmers must cultivate the habit of critical thinking.
o They should fix errors occurring in the code.
o Experience is another big skill for a programmer.
o Should know about working with databases by using SQL and other query languages.

Categories of Programming jobs

Web Developer

Web developers are the persons who are responsible for designing the web page,
maintaining the content, and looking after the performance of web site. Web Developers build
applications for the web by using languages like HTML, CSS, JavaScript, Python, and others. They
design interfaces like graphical user interfaces to communicate with the web pages. The web
developers combine various content into the page to meet the organization's needs.

These web developers are also categorized into three types, namely backend, front-end, and full-
stack developers.

1. Back-end web developers - The web developers who design a website template, develop
code and test the code to know the website's working are called backend developers.
Backend developers develop the server-side software. They mostly work with databases and
applications to give the data to the intended and verified users. They are the persons who
give different access levels to website content based on users' privileges.
2. Front-end web developers - The web developers who work and develop the visible part of
the website are called front-end developers. They work for client-side software. They design
the template of each page, including graphics, and use languages such as HTML, CSS, and
JavaScript to develop interactive pages for website users. For example, the pages visitors see
and interact with which is also known as the user interface.
3. Full-stack developers - The web developers who develop and work for both the backend
and front end are called full-stack developers. They develop client and server-side software.
These developers can build a fully working website and work for companies.

Full-stack developers = Front-end developer + Backend developer.

Web admins are also web developers who manage websites regularly. They are the persons who
update the websites and links of the web pages to redirect the users to the appropriate page
according to their requirements.

Mobile Developer

The app developer designs, develop, and implement mobile applications. Mobile developers
build applications for mobile phones (like smartphones and tablets). They are specialized in mobile
technology and build applications for Android, Apple (iOS applications), and Microsoft Windows
Phone platforms. The developers can use the android studio application to build the android apps
using Java Java, Kotlin, and flutter languages. Mobile developers also develop IOS applications for
macOS. Mobile developers are in huge demand because every person, from students to older
people, uses smartphones and their applications to meet their requirements.

Mobile developers should have good programming skills, especially related to object-oriented
concepts and problem-solving knowledge, so they try to build better applications that are helpful for
everyone.

Game Developer

The game developers build games compatible with mobiles, laptops, and desktops. They will
develop new ideas and build better games that may be related to education and entertainment.
They will make code from the ideas, which is a very creative task. These games can be simple to
create and some are even more complex to design and develop.

Designing skills, creativity, cognitive and analytical thinking, animation, computing, and problem-
solving skills are very important for game developers.

Computer System Engineer

Computer systems engineering includes designing computer networks, assembling and


managing networks and traffic for individuals and organizations, and managing software and
hardware components of computing systems. Computer system engineering includes various
interdisciplinary fields like computer science, electronic engineering, and mathematical analysis.

Computer System Engineers have much respect and need in government, transportation, and
Telecommunications industries to maintain the huge traffic and networks and maintain the
information flow between the senders and receivers.

Database Administrator
The database administrator maintains and operates the database. In these, the Database
architects design and develop the programs to meet the user requirements. They secure them from
various types of attacks.

What is the variation between computer programmers and software developers?

Software engineers are also computer programmers who program for a specific domain. The
software developers will think in depth about a project, like the design and features of an application
they are going to develop.

The programmers will write code for a specific problem rather than create a whole project
or website.

The software developers will create a featured application to meet the client's needs using
many concepts like object-oriented programming, functional programming, etc. But the
programmers will not consider the clients as they will not design the complete application. The
programmer always aims to deliver code for a particular problem instance.

The software developers will know computer programming, but only some of the
programmers can be software developers.

What is the difference between Computer Science and Computer Programming?

Computer programming includes creating and developing code to build new things to
eliminate problems and bring solutions by creating applications. The job roles under the computer
programming categories are software developer, web developer, mobile application developer, and
quality assurance (QA) analyst.

Coming to computer science is a field with no application development but involves solving
problems by applying abstract algorithms. Computer scientists develop ideas, and then computer
programmers implement those ideas by coding. The job roles which come under the computer
science field are software engineer, hardware engineer, system analyst, and software architect.
Hence without one field, the other cannot sustain.

How to choose a programming language for developing a project?

We came across the concept of computing programming, its uses, and different
programming languages and fields using programming for their enhancement. But one big question
which always roams in everyone's mind is which programming language should we select for a
particular project?

To answer this question, first, you need to gather some information about the project you will
develop. The information includes:

o What type of project are you going to develop?


o What is the intended goal of the project?
o What is the finance allocated for the project?
o How much time duration is required to complete it?
o What are the constraints applied to the project?
o What are the requirements and available resources for the project?

After the above survey and gathering the information, the project team members should choose
a language to program. Can take the following suggestions to select the language.

1. The first step is to know about the available programmers and their preferred language.
Then selecting according to their choice satisfies the project requirements and the
programmer's choice of programming language.

For example, when developing the backend, select the language the team knows better
when every language can fulfill your requirements. Should not select the language because
the language is popular and easy. We have to check whether it can meet the functionality of
our project.

2. Please choose a language so that even when you want to upscale the already-developed
project in the future, your language should support it.
3. Also, know about the client's preferential language, as after the project deployment, it will
be used by the clients themselves.
4. Check whether your chosen language supports the project's constraints like cost, schedule,
scope, quality, and resources.
5. Language speed, performance, and security are important in building a good project.
6. We may require more than one language to complete a project. In such situations, we
should choose languages compatible with each other.
7. Finally, language selection completely depends on the nature of the project (whether it is
related to web development or mobile development, etc.) and requirements. Some
languages may have very good frameworks to support your idea, while others may not, so
discuss with your group before moving further.

You may choose between JavaScript, HTML, and CSS. If you want to work on front-end
development.

You can select from JavaScript, C#, and Python when you work on backend development.

While working on android applications, use Java, flutter, or Kotlin.

While designing a project on web development, use object-oriented languages like Python,
Java, and C++.

If you work on machine learning and data science projects, use either python or R languages. The R
language supports statistical analysis and its visualization in a better way.

The above given are the only suggestions. If you are comfortable with other languages, which suit
your project, go with them.
How to learn to program?

To start with programming, first choose any basic language like C or C++ to understand the
programming concepts, the syntax of the language, and basic things like knowing about the
datatypes, operators, conditional statements, etc.

The best way to learn to program in less time is through self-practice itself. Then start
mastering algorithms and the data structures present in that language. While learning the
algorithms and data structures, take help from websites, books, and youtube videos to visualize the
concepts better. You can practice them in online sites and compilers, or you can even participate in
competitive programming available on various websites.

After you understand and learn the programming language, the next step is creating
small projects by applying the knowledge gained. If you can only partially develop a project, then at
least try to design small parts like creating a web page, building a calculator app, etc. The project
building boosts your confidence, and at the same time, you know how to apply the learned
programming language in real-time applications.

Then you can try complex projects and deep dive into emerging technologies like Machine
Learning, Artificial Intelligence, Deep learning, Cloud Computing, Mobile Development, Web
Development, etc. The more you explore, learn and practice, the more you gain and become
perfect.

Conclusion

The computer Programming field covers the various fields of computer science we
discussed. Programming is a developing and demanding career shortly. The only thing is to keep
updated with the emerging technologies and skills to stand strong in programming.
II. Programming tool
A programming tool is an application that helps developers create, test, optimize,
debug, and maintain other programs. It is designed to assist programmers in various tasks
throughout the software development life cycle. Programming tools can be part of a larger
single program called an IDE (integrated development environment) or run by themselves
from the command line.

Programming tools are categorized as different types based on their functions and
purpose, but the most basic are compilers, interpreters, and source code editors (like
Notepad++ and Atom). Some tools, like debuggers and profilers, are made with a specific
purpose.

List of programming tools


The following list contains several examples of applications that are considered to be
programming tools.

 Text editors - Text editors are uncomplicated tools designed for editing source code.
 Lint software - Lint software identifies syntax errors, deviations from industry
standard syntax, common programming errors, and potentially incorrect constructs.
It is specific to the programming language it's checking.
 Debuggers - Debugging tools help programmers identify and fix errors (bugs) in their
code.
 Version Control Systems - VCS tools, such as Git, help developers manage changes
to source code over time. They enable collaboration by allowing team members to
track and revert each other's changes using a system of pushes and pulls.
 Compilers and interpreters - Compilers translate source code to machine code or
some intermediate form, while interpreters can execute code directly. Examples
include GCC (GNU Compiler Collection), a compiler, and Python, an interpreted
language.
 Performance profilers - These tools help developers analyze the performance of
their code by identifying bottlenecks and resource-intensive areas.
 Database management tools - Tools like MySQL assist in the development,
querying, and management of databases.
Republic of the Philippines
President Ramon Magsaysay State University
(Formerly Ramon Magsaysay Technological University)
Iba, Zambales, Philippines
Tel/Fax No.: (047) 811-1683
OBJECT ORIENTED PROGRAMMING

JAVA OVERVIEW

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 has a large demand in the current job market
 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 QUICKSTART

 In Java, every application begins with a class name, and that class must match the filename.
 Let's create our first Java file, called Main.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:
JAVA SYNTAX

Every line of code that runs in Java must be inside a class. In our example, we named the class Main.
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.

1. The main Method

The main() method is required and you will see it in every Java program:

Any code inside the main() method will be


executed. remember that every Java program has a class name which must match the filename,
and that every program must contain the main() method.

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

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

System is a built-in Java class that contains useful members, such as out, which is short for "output".
The println() method, short for "print line", is used to print a value to the screen (or a file).

Don't worry too much about System, out and println(). Just know that you need them together to
print stuff to the screen.

You should also note that each code statement must end with a semicolon (;).

JAVA OUTPUT / PRINT

1. Print Text

You can add as many println() methods as you want. Note that it will add a new line
for each method:

 The Print() Method


There is also a print() method, which is similar to println().
The only difference is that it does not insert a new line at the end of the output:
Note that we add an extra space (after "Hello World!" in the example above), for better
readability.
 Double Quotes
When you are working with text, it must be wrapped inside double quotations marks "".
If you forget the double quotes, an error occurs:

2. Print Numbers
You can also use the println() method to print numbers.
However, unlike text, we don't put numbers inside double quotes:

You can also perform mathematical calculations inside the println() method:

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.

1. Single-line Comments
- 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:

This example uses a single-line comment at the end of a line of code:

2. Java Multi-line Comments


 Multi-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:

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

1. Declaring (Creating) Variables


To create a variable, you must specify the type and assign it a value:

Where type is one of Java's types (such as int or String), and variableName is the name of
the variable (such as x or name). The equal sign is used to assign values to the variable.

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

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

3. You can also declare a variable without assigning the value, and assign the value later:

4. Note that if you assign a new value to an existing variable, it will overwrite the previous
value:
2. Final Variables

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

3. Other Types
A demonstration of how to declare variables of other types:

JAVA PRINT VARIABLES

1. Display Variables
 The println() method is often used to display variables.

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

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

 For numeric values, the + character works as a mathematical operator (notice that we
use int (integer) variables here):
2. Declare Many Variables

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

 One Value to Multiple Variables

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

JAVA IDENTIFIERS

1. Identifiers
 All 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 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 _
 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
JAVA DATA TYPES

 a variable in Java must be a specified data type:

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

1. Primitive Data Types

A 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:

JAVA NUMBERS

Primitive number types are divided into two groups:

1. 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.
2. Floating point types represents numbers with a fractional part, containing one or more
decimals. There are two types: float and double.
 Integer Types
1. Byte
 The 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:

2. Short
 The short data type can store whole numbers from -32768 to 32767:

3. Int
 The 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.

4. Long
 The 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":

 Floating Point Types


- You should use a floating point type whenever you need a number with a decimal, such
as 9.99 or 3.14515.
- The float and double data types can store fractional numbers. Note that you should end
the value with an "f" for floats and "d" for doubles:

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

A floating point number can also be a scientific number with an "e" to indicate the power of
10:

 Java Boolean Data Types

Very often in programming, you will need a data type that can only have one of two values,
like:
- YES / NO
- ON / OFF
- TRUE / FALSE

For this, Java has a boolean data type, which can only take the values true or false:

 Java Characters

1. Characters
- The char data type is used to store a single character. The character must be
surrounded by single quotes, like 'A' or 'c':

2. Strings
- The String data type is used to store a sequence of characters (text). String
values must be surrounded by double quotes:

 Java Non-Primitive Data Types

Non-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 for String).
- 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.
- Examples of non-primitive types are Strings, Arrays, Classes, Interface, etc.
REFERENCES
What is Computer Programming - javatpoint

Programming Language | What is Programming Language - Javatpoint

What is a Programming Tool? (computerhope.com)

You might also like