Java Tutorial - 29 Methods in Java - StudyViral
Java Tutorial - 29 Methods in Java - StudyViral
A method is a set of code which is referred to by name and can be called (invoked) at any point in a program simply by utilizing the method's name.
Methods allow us to reuse the code without retyping the code.
The only required elements of a method declaration are the method's return type, name, a pair of parentheses, () , and a body between braces, {} .
The only required elements of a method declaration are the method's return type, name, a pair of parentheses,
()
, and a body between braces, {}
.More generally, method declarations have six components, in order:
- Modifiers—such as
public
,private
,protected - The return type—the data type of the value returned by the method, or
void
if the method does not return a value. - The method name—the rules for field names apply to method names as well, but the convention is a little different.
- The parameter list in parenthesis—a comma-delimited list of input
parameters, preceded by their data types, enclosed by parentheses,
()
. If there are no parameters, you must use empty parentheses. - An exception can be part of method header section after parentheses () and before opening brace {.
- The method body, enclosed between braces—the method's code, including the declaration of local variables, goes here.
Example :
public class DemoMethods {
//declaration of method
public static void myMethod() {
System.out.println("Hye it's my method");
}
//declaration of method
public void myMethod1() {
System.out.println("Hye now i need Object to call this method");
}
public static double adding1(double a, double b) {
double c = a+b;//30
return c;
}
//declaration of method
public int adding2(int a,int b,int c) {
int d = a+b+c;//17
return d;
}
public static void main(String[] args) {
myMethod();//calling
DemoMethods dm = new DemoMethods();
dm.myMethod1();
System.out.println("Retutn double :: "+adding1(10.0,20.0));
System.out.println("Retutn int :: "+dm.adding2(5,4,8));
}
}
Java Tutorial - 28 Constructor in Java - StudyViral
Java Tutorial - 28 Constructor in Java - StudyViral
Constructors
In Java Constructors are special methods which has the same name as class name.
It also helps to initialize the object of a class.
The new operator is used with constructor.
If we don't specify constructor than java compiler automatically provide it, we can also say Constructor is implicitly called by Java Compiler.
Constructor don't have return type.
There are two type of constructors in java.
1). Default constructor.
2). Parameterized Constructor.
Java Swing Tutorials (GUI) - Read and Write JList with Text File - Study Viral
Java Swing Tutorials (GUI) - Read and Write JList with Text File - Study Viral
In this video I am going to show You
1). Write data from JList to text file
2). Load/Add data to JList from text file
There are number of classes we can use
For this video we need
Save To File
we can use FileWriter but the problem is it don't allow new line character
BufferedWriter is used to read data
but i am using PrintWriter to write on text file because it has println()
Read From File
FileWriter - makes it possible to read the contents of a file as a stream
of characters.
BufferedReader - that reads text from a character-input stream,
buffering characters so as to provide for the efficient reading of
characters, lines and arrays.
Java Swing (GUI) - Update JList Data - Study Vira
Java Swing (GUI) - Update JList Data - Study Viral
In this video i am updating JList on runtime
after add data we select data from jlist
Now Update Selected Value from jtextfield to jlist
This thing add extra element to list
Now we need to remove selected index element also
Please Do Subscribe my Channel
Have A Nice Day :D
Java GUI - JList (Java Swing) Using Netbeans Part 02 - Study Viral
Java GUI - JList (Java Swing) Using Netbeans Part 02 - Study Viral
Now the Coding Part
1. Close button and Minimize Button
2. Button Color on Mouse Entered & Exit
3. Add button Code to add data to list
4. Simple click operation on jtextfield to clear msg
5. Increase Counter
6. Remove Data but before that we need to find index of jlist data which we
going to remove for that click operation on jlist
7. Now Remove data only when user select data from list
Java Tutorial - 27 Class & Objects in Java - StudyViral
Java Tutorial - 27 Class & Objects in Java - StudyViral
Class
A class is a user defined prototype or blueprint from which objects are created.
We can also say class is collection of data members and member functions/methods.
where data members are variable and member functions/methods are used to access
data members.
Keyword: class
Note: Every Single Java file can have only single/one public class.
We can't have two or more class as public within single java file.
Object
Objects are instance of class. In other words object reference are memory locations
where object data is saved.
Object of predefined or user defined classes can be created by using new operator.
Constructor
Constructors are special method which has same name as that of class name.
class Student{
String name;//data members
int rollno;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRollno() {
return rollno;
}
public void setRollno(int rollno) {
this.rollno = rollno;
}
//2 Member Functions
public void sleep() {
System.out.println(name+" "+rollno + " is Sleeping" );
}
public void study() {
System.out.println(name+" "+rollno + " is Studing" );
}
}
public class DemoClassObject {
public static void main(String[] args) {
Student s1;//s1 is object of Student Class
s1 = new Student();//Initialize
s1.setName("Hardeep");
s1.setRollno(101);
System.out.println("Name :: "+s1.getName());
System.out.println("Rollno :: "+s1.getRollno());
s1.sleep();
s1.study();
}
}
Java Tutorial - 26 Enhanced For Loop in Java - StudyViral
Java Tutorial - 26 Enhanced For Loop in Java - StudyViral
Enhanced for Loop in JAVA
Enhanced for loop is used to scan/iterate an Array instead of using traditional loops.
It was introduced in Java 1.5
We can also use it for iterate elements of Collection.
Enhanced is simple to use because it doesn't required condition to control the loop.
But it is inflexible beacause we can't use it to display individual element of array through index. It display all elements of array from first to last.
public class DemoEnchacedLoop { public static void main(String[] args) { String str[] = {"abc","def","ghi","jkl","mno","pqr"}; for(String s : str) { //System.out.println(s); } int ar[] = {1,2,3,4,5,6,7,8,9,10,11}; for(int i : ar) { //System.out.println(i); } int arr[][] = {{22,33,44},{55,66,77},{88,99,10}}; for(int a[]:arr) { for(int a1 : a) { System.out.print(a1+" "); } System.out.println(); } } }
Java Tutorial - 25 Array (2-Dimensional Array)in Java - Study Viral
Java Tutorial - 25 Array (2-Dimensional Array)in Java - Study Viral
2D (or 2 Dimensional Array)
Two-dimensional arrays are arrays of arrays.
Initializing two-dimensional arrays:
int[][] arr1 = new int[3][3];
The 1st dimension represent rows or number of one dimension, the 2nd dimension represent columns or number of elements in the each one dimensions.
int[][] arr2 = { {1,2,3}, {4,5,6}, {7,8,9} };
int[][] arr3 = new int[][] { {1,2,3}, {4,5,6}, {7,8,9} };
int[][] arr4 = new int[3][] { {1,2,3}, {4,5,6}, {7,8,9} };//error
You can initialize the row dimension without initializing the columns but not vice versa
int[][] x = new int[3][];
int[][] x = new int[][3]; //error
The length of the columns can vary for each row and initialize number of columns in each row.
int [][]x = new int [2][];
x[0] = new int[5];
x[1] = new int [5];
int [][]x = new int [3][];
x[0] = new int[]{0,1,2,3};
x[1] = new int []{4,5,6};
x[2] = new int[]{7,8,9,10,11};
Jagged array is array of arrays such that member arrays can be of different sizes,
i.e., we can create a 2-D arrays but with variable number of columns in each row.
These type of arrays are also known as Jagged arrays.
int x[] = {1, 2, 3};
int y[] = {6, 5, 4};
int z[] = {9, 8, 7};
int c[][]={x, y, z};
Java Tutorial - 24 Array (One Dimensional Array) in Java - Study Viral
An Array are continues memory allocation
which contains homogenous set of data.
An array is container object that holds a fixed
number of values of same/single type.
Array can be created as:
int arr1[] = {10,20,30,40,50};
int arr2[] = new int[] {10,20,30,40,50};
int arr3[] = new int[5];
we can find size of array by using
build-in property i.e. length
e.g. arr3.length;
which returns int value
NOTE : Array always begins with index zero
We can't access array element beyond the range
We can't resize the array but can use same
refernce variable to new refernce array
e.g. int ar[] = new int[5];
ar[] = new int[10];
ArrayCopy
To copy array elements from one element
to another array, we use a static method
arraycopy from System class
public static void arraycopy(ar1,s_index,ar2,s_index,ar1.length);
Java Tutorial 23 - Break and Continue in Java - Study Viral
Java Tutorial 23 - Break and Continue in Java - Study Viral
Break – When break statement encountered inside a loop then the loop is terminated and program control resumes with the next statement after the loop structure.
break;
Continue – When continue statement is encountered inside the loop with some condition then continue skipped that particular iteration and loop proceeds with next iteration.
continue;
public class DemoBreakContinue { public static void main(String[] args) { for(int i=1; i<=10 ;i++) { if(i==4) { continue; } System.out.println("i :: "+i); } System.out.println("wow i am out of loop"); } }
public class DemoBreakContinue { public static void main(String[] args) { for(int i=1; i<=10 ;i++) { if(i==4) { break; } System.out.println("i :: "+i); } System.out.println("wow i am out of loop"); } }
Java Tutorial 22 - Nested Loops in Java - Study Viral
A nested loop means a loop within a loop.
Or we can say an inner loop within the body of an outer loop.
A Clock is a fine example of nested loop where HOUR hand and MINUTE
hand all spin around the face of the clock.
The HOUR hand is like outer loop and MINUTE hand is like inner loop.
The HOUR hand only makes one revolution for every 60 of the MINUTE
hand’s revolution.(i.e. MINUTE hand increment it value by 1 ).
In case of pattern programs outer loop used for generating ROWS and inner
loop is used for generating COLUMNS for particular ROWS.
We can use it in patterns, 2D arrays and it just like GRID or table.
public class DemoNestedLoop { public static void main(String[] args) { for(int outer = 1;outer <=3 ; outer++) { for(int inner = 1; inner <=3 ; inner++) { System.out.println("outer :: "+outer +"\tinner ::"+inner); } System.out.println(); } for(int i = 0;i <= 5; i++) { for(int j = 0;j < i; j++) { System.out.print(" "+j); } System.out.println(); } } }
Java Tutorial 21- Loops in Java - Study Viral
Java Tutorial 21- Loops in Java - Study Viral
Loops are also known
as an iteration statements.
Loops are used to
repeat some set of code again and again until some condition is true.
In Java there are
FOUR type of loops.
- while loop
- do-while loop
- for loop
- Enhanced for loop
Loop Requirements
- Loop control variable
- Initialize the loop control variable
- Condition Check for loop
- Increment/Decrements
Syntax While Loop int a; // Loop Control Variable a = 0; // Initialize Variable while (a < 4) // Condition Check { System.out.println(“a :: ”+a); a++; // Increment } | Syntax do-while Loop int a; // Loop Control Variable a = 0; // Initialize Variable do{ System.out.println(“a :: ”+a); a++; //Increment } while (a < 4); // Condition Check |
||||||||||||||||||||||||
Syntax for Loop for( int i =0 ; i < 4 ; i++) { System.out.println(“a :: ”+a); } |
|
Java Tutorial 20 - Scanner Class - Study Viral
Java Tutorial 20 - Scanner Class - Study Viral
Scanner Class exists in java.util Package.
It is used to read input from keyboard and contains many methods which helps to store that input into primitive data-types as well as for String class objects.
Java Scanner class extends Object class and implements Iterator and Closeable interfaces.
A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods.
public byte nextByte( )
public short nextShort( )
public int nextInt( )
public long nextLong( )
public float nextFloat( )
public double nextDouble( )
public String next( )
public String nextLine( )
www.studyviral.in
Email : info@studyviral.in
Java Tutorial 19 - Switch Case Statement - Study Viral
Java Tutorial 19 - Switch Case Statement - Study Viral
This video show the use of Switch Case Statement.The switch statement is a multi-way branch statement. It provides an easy way to dispatch execution to different parts of code based on the value of the expression.
It can accepts byte, short, char, and int primitive data types.
It also works with enumerated types, the String class, and a few special classes that wrap certain primitive types: Character, Byte, Short, and Integer.
www.StudYViraL.in
E-mail : info@studyviral.in
Java Tutorial - 17 - If else Statement - Study Viral
Java Tutorial - 17 - If else Statement - Study Viral
Decision Making Statements (if-else)
The if-then statement is the most simple control flow statements. It helps your program to execute a certain block of code only if a particular test evaluates to true.
Type of if-else
if Statement
if else Statement
if else if Statement
Nested if - else
The if-then statement is the most simple control flow statements. It helps your program to execute a certain block of code only if a particular test evaluates to true.
Type of if-else
if Statement
if else Statement
if else if Statement
Nested if - else
===================================
class DecisionMaking{
public static void main(String []args){
if(true){
System.out.println("Yes its true");
}
System.out.println("Out side if ");
if(false){
System.out.println("Yes its again true");
System.out.println("out side if");
}
}
}
public static void main(String []args){
if(true){
System.out.println("Yes its true");
}
System.out.println("Out side if ");
if(false){
System.out.println("Yes its again true");
System.out.println("out side if");
}
}
}
==========================================
class DecisionMaking{
public static void main(String []args){
int a = 10;
int b = 20;
boolean bb = true;
Boolean bool = true;
if(bool){
System.out.println("Wow if can accept Wrapper Class object");
} else if((a>b) && bb){
System.out.println("Yes its true");
System.out.println("Yes a > b");
}else{
System.out.println("Yes its else part");
}
System.out.println("From Outside");
}
}
public static void main(String []args){
int a = 10;
int b = 20;
boolean bb = true;
Boolean bool = true;
if(bool){
System.out.println("Wow if can accept Wrapper Class object");
} else if((a>b) && bb){
System.out.println("Yes its true");
System.out.println("Yes a > b");
}else{
System.out.println("Yes its else part");
}
System.out.println("From Outside");
}
}
=================================================
class DecisionMaking{
public static void main(String []args){
int a = 10;
int b = 20;
boolean bb = true;
Boolean bool = new Boolean(false); //Wrapper Class Object
if(bool){
System.out.println("Wow if can accept Wrapper Class object");
} else if((a<b) && bb){
System.out.println("Yes its true");
System.out.println("Yes a > b");
}else{
System.out.println("Yes its else part");
}
System.out.println("From Outside");
}
}
public static void main(String []args){
int a = 10;
int b = 20;
boolean bb = true;
Boolean bool = new Boolean(false); //Wrapper Class Object
if(bool){
System.out.println("Wow if can accept Wrapper Class object");
} else if((a<b) && bb){
System.out.println("Yes its true");
System.out.println("Yes a > b");
}else{
System.out.println("Yes its else part");
}
System.out.println("From Outside");
}
}