0% found this document useful (0 votes)
17 views27 pages

Linux and Java Basics Workshop Guide

Uploaded by

skrewariya2675
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views27 pages

Linux and Java Basics Workshop Guide

Uploaded by

skrewariya2675
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CS202 : IT Workshop I (Java)

Course Instructor : Dr. Arijit Nath


Introduction to Linux Operating Systems
• Linux is based on UNIX OS.
• It is free and open source, accessible to everyone.
• This promotes global collaborations and innovations.
Architecture of Linux OS
• Kernel : A computer program that is the core of OS. It virtualizes the
common hardware resources and makes each process feel as if it is
the only process running.
• Shell : A shell is a program which provides an interface to user to use
OS services. It starts when the user logs in or starts the terminal.
✓Command line shell (terminal in linux)
✓Graphical shell

Kernel+ Supporting libraries and software packages=


Linux Distributions (like ubuntu, fedora, Debian etc)
Basic Linux Commands
File and Directory Management
• ls : Lists the contents of a directory
• pwd : Prints the current working directory
• cd : Changes the current directory to the specified directory
• mkdir : Creates a new directory
• rmdir : Removes an empty directory
• cp : Copies the files or directories
• mv : moves or renames files or directories
• rm : Deletes files or directories
• touch : Creates an empty file.
• cat : Displays the content of a file.
Basic Linux Commands
System Information and Utilities
• man [command] :Displays the manual page for a specified command.
• Grep [pattern][file]: Searches for a specified pattern within a file or
output.
• head [file] : Displays the beginning lines of a file.
• tail [file] : Displays the ending lines of a file.
• df : reports the disk space usage
Permission and Ownership
• Every file and directory is associated with permission and ownership.
• Helps in maintaining security and control in multi-user environments.
• Each file or directory has three types of ownership.
✓Owner : The user who creates the file.
✓Group : Each file belongs to a group.
✓Others (world) : Any user in the system who is not the owner and not in the
group.

-rw-r--r-- 1 alice devs 1234 Aug 6 12:34 [Link]

Owner Group
Permission
• Each of the three user classes (owner, group, others) can have the
following permissions.

Symbol Permission Description


r Read View contents
w Write Modify contents
x Execute Run the file (or enter directory)
Two Important Commands
• chmod (change mode) : used to change permissions of a file or
directory.
1. Symbolic Mode : chmod u+w [Link]
2. Numeric Mode : chmod 775 [Link] (Giving read and write to others)
• Chown (Change owner) : Used to change the ownership of a file or
directory.
1. chown bob [Link] # Change owner to 'bob'
2. chown bob:staff [Link] # Change owner to 'bob' and group to 'staff'
Shell Script
• A shell script is a text file containing a sequence of commands that
are run by the shell (like bash, sh, zsh, etc.).
• It's a way to automate tasks you would normally type manually in the
terminal.

#!/bin/bash # <-- This is called a shebang; it tells the system to use bash to
run this file
echo "Hello, world!" # <-- A simple command
Last Class Summary
Java Code ([Link] )

Compiler
(javac)

Byte Code ([Link] )

JVM JVM JVM

Windows Linux Mac


First Java Program
public class HelloWorld{
public static void main ( String [ ] args ){
[Link] .println (" Hello , World !");
}
}
First Java Program
public class HelloWorld{
public static void main ( String [ ] args ){
System . out . println (" Hello , World !");
}
}

• JVM looks for the main(String[] args) method to use it as the


entry point of execution.
• Hello World is the driver class hence, name of the java program
should be [Link]
First Java Program Returns
nothing

public class HelloWorld{


public static void main ( String [ ] args ){
System . out . println (" Hello , World !");
}
}

JVM can invoke


JVM can access it main without
from outside of the creating objects
class
• Use of public : The method is accessible from outside the class. If
main is not public, JVM will not be able to find main and execute.
• Use of static : The method main belongs to the class HelloWorld, not
to the instances of the class. JVM runs main() without creating an
object. So , it must be static.
• Use of void : main() does not return anything to the JVM.
• Use of String[] args: main() accepts an array of strings as command
line argument.
How to Compile and Execute
• To Compile
javac [Link] (Generates the byte code)
• To Execute the Program
java HelloWorld (JVM interpreter produces the machine code)
Displaying on Console
class HelloWorld{
public static void main ( String [ ] args ){
[Link] (" Hello , World !");
[Link] (" Hello \n World !");
[Link] (“%s%s", "Hello", "World");
}
}
Good Programming Practice With Java
1. Naming
• Class (Pascal Convention): begin with a capital letter, capitalize first letter of each
subsequent word (e.g., Circle, HelloWorld)
• Variable (camel Convention) : begin with a small letter, capitalize first letter of
each subsequent word (e.g., radius, isColored)
• Functions (camel Convention) : begin with a small letter, capitalize first letter of
each subsequent word (e.g., draw(), fillColor()) .
2. Readability
• Meaningful names to class, variable, method, etc.
• One variable in one line
• Proper indentation
• Constant should be given proper name (named constants)
• (e.g. ARRAY_LENGTH instead of 10)
Variables in Java
• Two Types : Primitive and Non-primitive
• Primitive types are int, float, double
• Non-primitives are array, class, string etc
Command Line Arguments
• Java command-line argument is an argument, i.e., passed at the time of
running the Java program.
• For example, java Geeks Hello World.
• Here, the words Hello and World are the command-line arguments. JVM
will collect these words and will pass these arguments to the main
method as an array of strings called args. The JVM passes these
arguments to the program inside args[0] and args[1].
Working of Command-Line Arguments

• Command-line arguments in Java are space-separated values passed


to the main(String[] args) method.
• JVM wraps them into the args[] array, where each value is stored as a
string (e.g., args[0], args[1], etc.).
• The number of arguments can be checked using [Link].
Reading Input from Console (Finding Sum)
import [Link]; Importing class Scanner from the package util
public class Addition{
public static void main( String[] args ){
Scanner input = new Scanner( [Link] ); Creating object of scanner class
int number1;
int number2;
int sum;
[Link]( "Enter first integer: " );

number1 = [Link]();
Reading from keyboard
[Link]( "Enter second integer: " );

number2 = [Link]();
[Link]();
sum = number1 + number2;
[Link]("Sum is: "+sum);
}
How about writing the same program using
command-line arguments (String[] args)
instead of Scanner?
Reading Input from Command-line (Finding
Sum)
public class Addition { public static void main(String[] args)
{ // Check if the user provided exactly two arguments
if ([Link] != 2) {
[Link]("Please provide exactly two integers as
arguments."); return;
}
// Convert String arguments to integers
int number1 = [Link](args[0]); Converting the command line
argument from string to integer
int number2 = [Link](args[1]);
int sum = number1 + number2;
[Link]("Sum is: " + sum); }}
How to execute the previous code?
1. Compile the program -> javac [Link]
2. Run with arguments -> java Addition 10 20
Conditional Statements in Java
public class Addition{
public static void main( String[] args ){
Scanner input = new Scanner( [Link] );
int number1, number2;
[Link]( "Enter first integer: " );
number1 = [Link]();
[Link]( "Enter second integer: " );
number2 = [Link]();
[Link]();
if(number1>number2)
[Link](number1+” is less than ”+number2 );
else
[Link]( number1+” is greater than or equal to
”+number2 );
Loop and Switch
switch(number) {
case 0:
[Link]("You typed zero");
break;
case 1:
[Link]("You typed one");
break;
default:
[Link]("Neither zero nor one");
}

You might also like